Wordpress 数据库查询错误 Call to a member function get_results() on null

Posted ryanzheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wordpress 数据库查询错误 Call to a member function get_results() on null相关的知识,希望对你有一定的参考价值。

在插件中的一个文件使用如下代码,无法查询

<body>   
        <?php
        global $wpdb;
        $sql = "";
        $sql = "SELECT * FROM emgAdmin";
        $sql = "$sql INNER JOIN wp_posts ON wp_posts.ID = emgAdmin.ID_OfPost ";        
        $ArrResult = $wpdb->get_results($sql);
        while ($emgAdminResult = mysql_fetch_object($ArrResult)) {
            echo($emgAdminResult->post_date);
        }
        ?>
    </body>

错误如下:

技术图片

 

处理如下:

  1. 删除 global $wpdb
  2. 将以下代码添加到文件顶部
require_once( $_SERVER[DOCUMENT_ROOT] . /wp-config.php );
require_once( $_SERVER[DOCUMENT_ROOT] . /wp-includes/wp-db.php );

 

完整代码如下:

<?php
require_once( $_SERVER[DOCUMENT_ROOT] . /wp-config.php );
require_once( $_SERVER[DOCUMENT_ROOT] . /wp-includes/wp-db.php );
$sql = "";
$sql = "SELECT * FROM emgAdmin";
$sql = "$sql INNER JOIN wp_posts ON wp_posts.ID = emgAdmin.ID_OfPost ";        
$ArrResult = $wpdb->get_results($sql);
while ($emgAdminResult = mysql_fetch_object($ArrResult)) {
    echo($emgAdminResult->post_date);
}
?>

 

以上是关于Wordpress 数据库查询错误 Call to a member function get_results() on null的主要内容,如果未能解决你的问题,请参考以下文章

Call to a member function free_result() on a non-object

Cython 将扩展模块传递给 python 导致 to_py_call_code 错误

QT编程错误解决 error: no matching function for call to

QT编程错误解决 error: no matching function for call to

Call to undefined function怎么解决

Call to undefined function怎么解决?