-1

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);   
  • 1
    You need to pass in a connection, not just the query here: `$rs_results_site_d = mysqli_query($sql_site_d) or die(mysqli_error());` this looks like Wordpress, so you'd likely need `$rs_results_site_d = mysqli_query($wpdb, $sql_site_d) ...` – WillardSolutions May 21 '18 at 16:17
  • *"Sorry for the duplicate question ... none of the solutions seem to be working."* There are several posts on SO about this error. Please explain how they did not help here. – faintsignal May 21 '18 at 16:21
  • When using Wordpress, it already gives you the database query functionality that you need. Use them instead. here: [$wpdb](https://stackoverflow.com/q/39758937/4365678) – Ibrahim Mohamed May 21 '18 at 16:27
  • Most (if not all) solutions advocate for "passing in a connection," so I edited line 120 to $rs_results_site_d = mysqli_query($con, $sql_site_d) or die(mysqli_error()); Same error! PS: Am not a programmer, depending on trial and error! – Anthony Muchangi May 21 '18 at 16:27
  • Then the next step would be to ensure the value of `$con` is correct. Seems it would be `null` based on your error message. You can confirm that with `var_dump($con);` – faintsignal May 21 '18 at 16:29
  • @AnthonyMuchangi consider using [wpdb](https://codex.wordpress.org/Class_Reference/wpdb) instead. Otherwise you will have to create a new mysqli connection and pass it as the first argument in the mysqli_ functions you are using. But Wordpress provides a connection already so no need for redoing it (bad practice) – Ibrahim Mohamed May 21 '18 at 16:38
  • 1
    @IbrahimShendy I have adjusted that but still no change. Here's the whole code: [link](https://ufile.io/b48i4) – Anthony Muchangi May 21 '18 at 16:58

1 Answers1

0

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

Ibrahim Mohamed
  • 433
  • 4
  • 14