我正在学习开发一个自定义插件,目前它的目的是从wp数据库读取数据并显示数据。
我相信我最初的SQL语句是正确的,因为它获取了我想要显示的数据。然而,每当执行该函数时(从页面加载时的短代码),我都会得到错误,而不是显示提取的数据Trying to get property \'num_rows\' of non-object in C:\\wamp64\\www\\testsite1\\wp-content\\plugins\\WickCustomLD\\WickCustomLD.php on line 84
我已经通过wpDataTables插件测试了SQL语句,它使用几行数据正确地访问了正确的数据,但很明显,我的php获取代码存在一些问题。
我们将非常感激您的任何帮助。
我的代码:
<?php
/**
* @package WickCustomLD
*/
/*
Plugin Name: WickCustomLD
Plugin URI: https://url.com
Description: This plugin provides extra functionality to the LeanDash plugin
Version: 1.0.0
Author: Sam Wickins
Author URI: https://url.com
Licence: GPLv2 or later
Text Domain: WickCustomLD-plugin
*/
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
defined( \'ABSPATH\' ) or die(\'You do not have access to these files.\');
class WickCustomLDStart
{
//constructor
function __construct()
{
add_action(\'init\', array ($this, \'custom_post_type\'));
}
//methods
function activate()
{
$this -> custom_post_type();
flush_rewrite_rules();
}
function deactivate()
{
flush_rewrite_rules();
}
function custom_post_type()
{
register_post_type( \'book\', [\'public\' => true, \'label\' => \'Books\'] );
}
function ld_cat_data_process()
{
$userID = get_current_user_id();
$sql ="
SELECT wp_users.`user_email`,
wp_wp_pro_quiz_statistic.`correct_count`,
wp_wp_pro_quiz_statistic.`incorrect_count`,
wp_wp_pro_quiz_category.`category_name`
FROM wp_users
INNER JOIN wp_wp_pro_quiz_statistic_ref
ON wp_users.`ID` = wp_wp_pro_quiz_statistic_ref.`user_id`
INNER JOIN wp_wp_pro_quiz_statistic
ON wp_wp_pro_quiz_statistic_ref.`statistic_ref_id` = wp_wp_pro_quiz_statistic.`statistic_ref_id`
INNER JOIN wp_wp_pro_quiz_question
ON wp_wp_pro_quiz_statistic.`question_id` = wp_wp_pro_quiz_question.`id`
INNER JOIN wp_wp_pro_quiz_category
ON wp_wp_pro_quiz_question.`category_id` = wp_wp_pro_quiz_category.`category_id`
WHERE wp_users.`ID` = 1";
global $wpdb;
$result = $wpdb->get_results($sql);
if ($result->num_rows > 0) {
//output data of each row
while($row = $result->fetch_assoc()) {
echo "Email: " . $row["user_email"] . "Correct Count: " .
$row["correct_count"] . "Incorrect Count: " .
$row["incorrect_count"] . "Category: " .
$row["category_name"] . "<br>";
}
}
else {
echo "0 results i\'m afraid! Sorry about that!";
}
}
}
if ( class_exists( \'WickCustomLDStart\' ) )
{
$WickCustomLDInit = new WickCustomLDStart();
add_shortcode( \'MyShortcode\', array( $WickCustomLDInit, \'ld_cat_data_process\' ) );
}
//activation
register_activation_hook( __FILE__, array( $WickCustomLDInit, \'activate\' ) );
//deactivation
register_deactivation_hook( __FILE__, array( $WickCustomLDInit, \'activate\' ) );
?>
最合适的回答,由SO网友:Sally CJ 整理而成
$wpdb->get_results()
返回查询表中的行数组,而不是mysqli_result 对象,因此您可以使用foreach
像这样:
$result = $wpdb->get_results( $sql );
foreach ( $result as $row ) {
echo $row->correct_count;
// ...
}
默认情况下,每一行都是一个包含列的对象(
<column name> => <column value>
) 但如果希望该项是关联数组,则传递
ARRAY_A
作为的第二个参数
$wpdb->get_results()
.
$result = $wpdb->get_results( $sql, ARRAY_A );
foreach ( $result as $row ) {
echo $row[\'correct_count\'];
// ...
}
请参阅
reference 了解更多信息。