php - Issue with fetching mysql data and displaying results via shortcode in webpage

I am learning to develop a custom plugin which currently has the purpose of reading data from the wp database and displa

I am learning to develop a custom plugin which currently has the purpose of reading data from the wp database and displaying the data.

I believe my initial SQL statement is correct in that it fetches the data that I want to display. However, whenever the function is executed (from the shortcode on page load), instead of it displaying the fetched data, I get the error Trying to get property 'num_rows' of non-object in C:\wamp64\www\testsite1\wp-content\plugins\WickCustomLD\WickCustomLD.php on line 84

I have tested the SQL statement via a wpDataTables plugin and it correctly accesses the correct data with several lines of data, but clearly there is some issue with my php fetch code.

Any help would be very gratefully received.

My Code:

<?php
/**
* @package WickCustomLD
*/

/*
Plugin Name: WickCustomLD
Plugin URI: 
Description: This plugin provides extra functionality to the LeanDash plugin
Version: 1.0.0
Author: Sam Wickins
Author URI: 
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 </>.
*/

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' ) );


?>

I am learning to develop a custom plugin which currently has the purpose of reading data from the wp database and displaying the data.

I believe my initial SQL statement is correct in that it fetches the data that I want to display. However, whenever the function is executed (from the shortcode on page load), instead of it displaying the fetched data, I get the error Trying to get property 'num_rows' of non-object in C:\wamp64\www\testsite1\wp-content\plugins\WickCustomLD\WickCustomLD.php on line 84

I have tested the SQL statement via a wpDataTables plugin and it correctly accesses the correct data with several lines of data, but clearly there is some issue with my php fetch code.

Any help would be very gratefully received.

My Code:

<?php
/**
* @package WickCustomLD
*/

/*
Plugin Name: WickCustomLD
Plugin URI: https://url
Description: This plugin provides extra functionality to the LeanDash plugin
Version: 1.0.0
Author: Sam Wickins
Author URI: https://url
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/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' ) );


?>
Share Improve this question edited Jun 8, 2020 at 13:39 sw123456 asked Jun 8, 2020 at 13:31 sw123456sw123456 1318 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

$wpdb->get_results() returns an array of rows from the queried table and not a mysqli_result object, so you can just use foreach like so:

$result = $wpdb->get_results( $sql );

foreach ( $result as $row ) {
    echo $row->correct_count;
    // ...
}

And by default, each row is an object with the columns (<column name> => <column value>) in the database table, but if you want the item be an associative array, then pass ARRAY_A as the second parameter for $wpdb->get_results().

$result = $wpdb->get_results( $sql, ARRAY_A );

foreach ( $result as $row ) {
    echo $row['correct_count'];
    // ...
}

Please refer to the reference for further information.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742367385a4430588.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信