PHP sql with foreach loop variable problem

Posted by anthony on Stack Overflow See other posts from Stack Overflow or by anthony
Published on 2010-06-11T19:26:58Z Indexed on 2010/06/11 19:32 UTC
Read the original article Hit count: 220

Filed under:
|
|

This is really getting frustrating. I have a text file that I'm reading for a list of part numbers that goes into an array. I'm using the following foreach function to search a database for matching numbers.

$file = file('parts_array.txt');

foreach ($file as $newPart)
{
    $sql = "SELECT products_sku FROM products WHERE products_sku='" . $newPart . "'";
    $rs = mysql_query($sql);
    $num_rows = mysql_num_rows($rs);

    echo $num_rows;
    echo "<br />";

}

The problem is I'm getting 0 rows returned from mysql_num_rows. I can type the sql statement without the variable and it works perfectly. I can even echo out the sql statement from this script, copy and paste the statement from the browser and it works. But, for some reason I'm not getting any records when I'm using the variable. I've used variables in sql statements tons of times, but this really has me stumped.

© Stack Overflow or respective owner

Related posts about php

Related posts about sql