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 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:wamp64wwwtestsite1wp-contentpluginsWickCustomLDWickCustomLD.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:

* @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
    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

    function __construct()
        add_action('init', array ($this, 'custom_post_type'));      


    function activate()
        $this -> custom_post_type();

    function deactivate()

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

register_activation_hook( __FILE__, array( $WickCustomLDInit, 'activate' ) );

register_deactivation_hook( __FILE__, array( $WickCustomLDInit, 'activate' ) );

, , sw123456 3 years 2020-06-08T09:10:23-05:00 0 Answers 87 views 0

Leave an answer