简体   繁体   中英

$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 ?

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM