$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Foreach loop using $wpdb not results from rows|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Foreach loop using $wpdb not results from rows

matteradmin11PV0评论

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

?>

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

?>
Share Improve this question edited Feb 10, 2019 at 15:01 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 10, 2019 at 14:54 SchmutlySchmutly 391 gold badge2 silver badges9 bronze badges 2
  • What do $product and $serial variables contain? – Krzysiek Dróżdż Commented Feb 10, 2019 at 15:03
  • The software being activated sends the software title and email to this script which also uses the Slim framework and if it matches the 'same' from new table in WP database sends an echo back to the software..if it matches echo's true and software is activated, if false says not activated and to contact support. it works outside of WordPress..im trying to bring it into wordpress simple plugin to make it easier and have no need to create a separate DB in cpanel and use a connect.php file. Here is original file-> pastebin/embed_js/tYapfAw6 – Schmutly Commented Feb 10, 2019 at 22:04
Add a comment  | 

1 Answer 1

Reset to default 1

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far