Forum Moderators: coopster

Message Too Old, No Replies

mysql query built from array

         

mschultem

8:59 am on Oct 3, 2007 (gmt 0)

10+ Year Member



hi,
i want to query a database for a number of ids (pids) with a query partly built from an array.
i get the pids through:
$pids = explode("\r", $_GET[pids]);

and tried:

for ($i = 0; $i <= count($pids); $i++) {
//echo $pids[$i];

$query = "SELECT byear, sex FROM demography WHERE pid='$pids[$i]'";
$result= mysql_query($query);
while($row = mysql_fetch_array($result))
{

//echo $row['byear']."&nbsp;".$row['sex'];
echo "<br>";
}
}

which does not work - it gives me only one result for the first field in the array. however it works if i define the array in the script without the explode:
$pids1 = array("d3ecf7f3265b", "2e714bd65311", "16c60bd98f33", "ac06524701e7");
the following print commands:
print_r($pids);
print_r($pids1);

show me the same arrays.

is there a difference i can not see through the print commands?

thanks
m

joelgreen

9:44 am on Oct 3, 2007 (gmt 0)

10+ Year Member



try trimming pids before using. There may be \n character

$query = "SELECT byear, sex FROM demography WHERE pid='".trim($pids[$i])."'";

mschultem

9:54 am on Oct 3, 2007 (gmt 0)

10+ Year Member



great!
thanks.