By Schmutly


2019-02-10 14:54:42 8 Comments

I'm using $wpdb to develop a plugin to check for some row fields to see if they match and echo true or false. My original code was working when it was a separate mysql DB and accessed that way. As its within wordpress plugin i needed to use $wpdb.

Im selecting from a separate table ( ipn_data_tbl ) and the wpdb prefix gets appended correctly. I want to check each row in that table and match where the item_name and payer_email are check from a software that gets activated. It all worked on stand alone DB outside of wordpress but now i've added the $wpdb (incorrectly?) its not working.

I don't think i am using it correct..any ideas? :

<?php



require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
    $ipn_table = $wpdb->prefix ."ipn_data_tbl";


   global $wpdb,$ipONactivate,$ipn_table;



$sql = "SELECT * FROM `$ipn_table` WHERE `item_name` = '$product' && `payer_email` = '$serial'";
$result = $wpdb->get_results($sql)or die(mysql_error());  //$wpdb->get_results

    if ($result->num_rows != 0) {
        foreach($result as $row){

        if ($row[`item_name`] == $product && $row[`payer_email`] == $serial) {
            echo true;
             } else {
            echo false;
                    }
                }
            }

?>

1 comments

@Krzysiek Dróżdż 2019-02-10 15:12:52

First of all, you should never concatenate SQL Query with any variables this way - it will cause SQL Injection vulnerability.

Also... get_results method returns just an array of results. You can’t use it as an object and get num_rows from it - there is no such property in an array (it’s a property of WPDB object).

Another problem is that by default get_results will return selected rows in object format, not as arrays.

So here’s the fixed code:

<?php
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

    global $wpdb, $ipONactivate;

    $ipn_table = $wpdb->prefix ."ipn_data_tbl";

    $result = $wpdb->get_results( $wpdb->prepare(
        "SELECT * FROM {$ipn_table} WHERE item_name = %s AND payer_email = %s",
        $product, $serial
    ) );

    if ( ! empty( $result ) ) {
        foreach($result as $row) {

            if ($row->item_name == $product && $row->payer_email == $serial) {
                echo true;
            } else {
                echo false;
            }
        }
    }
?>

BTW. You can trust the SQL. If you select only rows that has given product and serial, then you don’t have to loop through them and check if they are equal to given values.

@Schmutly 2019-02-13 07:26:44

Thanks Krzysiek..i had an obvious typo i didnt see. I copy/pasted your coode but had to add a bit more..but got it going. Thanks again..you ROCK mate :)

@Krzysiek Dróżdż 2019-02-13 07:28:33

@Schmutly yeah, I’m pretty sure it’s just a simplified version of what you needed in the end - it was meant to show the idea how to loop through these results

Related Questions

Sponsored Content

1 Answered Questions

How to Update multiple rows using $wpdb->update

  • 2018-01-29 07:34:14
  • Minesh
  • 771 View
  • 0 Score
  • 1 Answer
  • Tags:   wpdb

1 Answered Questions

[SOLVED] $wpdb->update multiple rows, like IN in normal SQL

  • 2014-08-01 14:08:52
  • Ludvig Sjöbeck
  • 7973 View
  • 6 Score
  • 1 Answer
  • Tags:   wpdb

1 Answered Questions

[SOLVED] Updating all rows of table with $wpdb

  • 2018-06-11 17:37:07
  • 12Hannibal12
  • 784 View
  • 1 Score
  • 1 Answer
  • Tags:   database wpdb

4 Answered Questions

[SOLVED] How to parse row results from $wpdb -> get_results

  • 2011-07-19 21:42:35
  • redconservatory
  • 86120 View
  • 8 Score
  • 4 Answer
  • Tags:   wpdb

0 Answered Questions

WPDB delivers wrong results from complex queries

2 Answered Questions

[SOLVED] $wpdb->insert - inserting multiple rows

  • 2011-09-02 05:38:38
  • Albin Joseph
  • 9834 View
  • 1 Score
  • 2 Answer
  • Tags:   wpdb

1 Answered Questions

[SOLVED] wpdb query not returning results containing single quotes

  • 2015-07-30 08:00:23
  • CharlesStamp
  • 1020 View
  • 0 Score
  • 1 Answer
  • Tags:   wpdb

1 Answered Questions

[SOLVED] Can't update multiple rows with $wpdb query

  • 2014-08-19 15:53:39
  • Neovea
  • 719 View
  • 0 Score
  • 1 Answer
  • Tags:   php query wpdb

1 Answered Questions

wpdb->insert adds too many rows

0 Answered Questions

$wpdb->get_results() returns good result only in first foreach iteration

  • 2012-10-16 12:00:17
  • Paul
  • 1986 View
  • 1 Score
  • 0 Answer
  • Tags:   wpdb

Sponsored Content