Sobhan Khoshdast
Sobhan Khoshdast

Reputation: 11

$wpdb not working, doesn't show anything in posts

I was looking for using wpdb to fetch data from database and also display on a post in Wordpress. Unfortunately, I could not to do that and wpdb not working for me. this is my code :

<?php
    $results= $wpdb->get_results ("SELECT Product_Name from print_drug");
    foreach ($results as $obj){
      echo $obj->Product_Name;
    }
?>

but after save then preview, i can see nothing on a page to show and i think wpdb->get_results return a null array. :)

and also i used this following code to see my returned array is empty or not:

if (!empty($results) {
echo "1";
} else {
echo "2";
}

but i didn't see anything on a page and no output was there.

what should i do ?

Upvotes: 1

Views: 2102

Answers (1)

Mike D.
Mike D.

Reputation: 175

I see you are using a plugin for inserting snippets of PHP code into the posts/pages' contents. If you want to know what is happening with your query, insert the following piece of code into the page's content:

[insert_php]
global $wpdb;
$wpdb->show_errors();
$results = $wpdb->get_results("SELECT Product_Name from print_drug");
$wpdb->print_error();
$wpdb->hide_errors();
[/insert_php]

If your WordPress is a multisite installation, use the piece of code:

[insert_php]
if(!defined('DIEONDBERROR')) define( 'DIEONDBERROR', true );
global $wpdb;
$wpdb->show_errors();
$results = $wpdb->get_results("SELECT Product_Name from print_drug");
$wpdb->print_error();
$wpdb->hide_errors();
[/insert_php]

Upvotes: 2

Related Questions