PHP Warning: mysqli_query() expects parameter 1 to

2019-09-19 18:04发布

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.

Error Screeshot

Here's the code:

// 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);   

标签: php mysqli
1条回答
霸刀☆藐视天下
2楼-- · 2019-09-19 19:11

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

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);  
            ....
        }
    }
}

Modified lines:

Line 4: global $post, $wpdb;

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

查看更多
登录 后发表回答