WordPress: Passing variable into $wpdb returns NULL

I am having a really strange issue with my code. I am trying to grab a row from the database using $wpdb-get_row, but it doesn’t work when I’m grabbing the value from the CSV.

In my below code, when I pass $unqiue_id into the query, it returns NULL. $unqiue_id comes from the CSV and is properly echoing the correct numbers.

If I create my own variable and manually set the ID, like I did in the below code for $manual_id, it works.

Why doesn’t $unique_id work?

global $wpdb;
    $error_msg = '';
    if( isset($_POST['sbmt'])) {
        $ext = substr($_FILES['selectcsv']['name'],(strlen($_FILES['selectcsv']['name'])-3),3);
        if($ext!="csv") {
            $error_msg = 'Please select only CSV file';
        } else {
        $selectcsv = $_FILES['selectcsv'];
        if (($handle = fopen($selectcsv['tmp_name'], "r")) !== FALSE) { 
            fgetcsv($handle, 0);
            while (($row = fgetcsv($handle, 0, ",")) !== FALSE) {
                $unqiue_id = (int)$row[89]; //echo $unique_id will display 12
                $manual_id = 12;
                $get_row1 = $wpdb->get_row("SELECT * FROM users WHERE id=".$unqiue_id);
                $get_row2 = $wpdb->get_row("SELECT * FROM users WHERE id=".$manual_id);
                var_dump($get_row1); //returns NULL
                var_dump($get_row2); //returns the correct results.         
            }           
            fclose($handle);

        }
      }
    }

If I run this code with standard mysql, it works:

$get_row_result = mysql_query("SELECT * FROM users WHERE id=".$unqiue_id);
$get_row = mysql_fetch_array($get_row_result);
$get_row = $prior_rate["id"];
var_dump($get_row); //returns the correct results.  

Why doesn’t it work with $wpdb???


Source: wpdb

Leave a Reply