本文介绍了PHP警告:mysqli_query()期望参数1为mysqli,给定null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉重复这个问题,但是有人可以帮我解决这个问题吗?是PHP新手,所有解决方案似乎都不起作用.

Sorry for the duplicate question but can someone please help me solve this? Am a PHP newbie and none of the solutions seem to be working.

错误记录

这是代码:

// Add Signature Image after single post and page
add_filter('the_content','add_signature', 1);
function add_signature($text) {
global $post;
if(($post->post_type == 'post') || ($post->post_type == 'page')){
//$sql_site_d = "select * from orders_discounts";
$sql_site_d = "select * from orders_discounts where url = 'homeworkmaid.com' and status =1";

$rs_results_site_d = mysqli_query($sql_site_d) or die(mysqli_error());
$total_site_d = mysqli_num_rows($rs_results_site_d);
if ($total_site_d > 0){
$row_site_d = mysqli_fetch_array($rs_results_site_d);

推荐答案

由于您使用的是Wordpress,因此建议您使用它们的$wpdb全局对象,可以通过首先全局调用它来使用它:global $wpdb;.然后,您将拥有像$wpdb->get_results( 'query', output_type );这样使用的函数,而不是mysqli函数. 请参阅WP Codex

Since you are using Wordpress, I advise you to use their $wpdb global object which you can use by first calling it globally: global $wpdb;. Then you will have functions to use like $wpdb->get_results( 'query', output_type ); instead of the mysqli functions. See the WP codex

但是,如果您想使用mysqli_函数,您仍然可以使用$wpdb,它具有一个mysqli对象,您可以通过以下方式访问它:$wpdb->dbh.这将是mysqli_函数所需的mysqli连接对象.

But if you would like to use mysqli_ functions you still can use $wpdb which have a mysqli object which you can access by: $wpdb->dbh. That will be your mysqli connection object that you need for mysqli_ functions.

要将其应用于您的代码:

To apply this to your code:

// Add Signature Image after single post and page
add_filter('the_content','add_signature', 1);
function add_signature($text) {
    global $post, $wpdb;
    if(($post->post_type == 'post') || ($post->post_type == 'page')){
        //$sql_site_d = "select * from orders_discounts";
        $sql_site_d = "select * from orders_discounts where url = 'homeworkmaid.com' and status =1";

        $rs_results_site_d = mysqli_query($wpdb->dbh, $sql_site_d) or die(mysqli_error($wpdb->dbh));
        $total_site_d = mysqli_num_rows($rs_results_site_d);
        if ($total_site_d > 0){
            $row_site_d = mysqli_fetch_array($rs_results_site_d);
            ....
        }
    }
}

修改的行:

第4行: global $post, $wpdb;

第9行: $rs_results_site_d = mysqli_query($wpdb->dbh, $sql_site_d) or die(mysqli_error($wpdb->dbh));

这篇关于PHP警告:mysqli_query()期望参数1为mysqli,给定null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:26
查看更多