简体   繁体   English

$wpdb 不起作用,帖子中不显示任何内容

[英]$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.我正在寻找使用 wpdb 从数据库中获取数据并显示在 Wordpress 中的帖子上。 Unfortunately, I could not to do that and wpdb not working for me.不幸的是,我不能这样做,wpdb 对我不起作用。 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.但是在保存然后预览之后,我在页面上看不到任何内容,我认为 wpdb->get_results 返回一个空数组。 :) :)

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.我看到您正在使用一个插件将 PHP 代码片段插入到帖子/页面的内容中。 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:如果您的 WordPress 是多站点安装,请使用以下代码:

[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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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