View Full Version : Pleae rectify my code
hassan
09-25-2008, 07:39 AM
Iam trying to compare a row with array of values in php as follows
$array[0]
$array[1]
.
.
.
.
.
$array[n]
$result=mysql_query("select * from databasetable where row1=any($array)");
the above code is not working. Please any body solve my problem.
schorhr
09-25-2008, 07:55 AM
Are you looking for
foreach (array_expression as $value)
foreach (array_expression as $key => $value)
?
$stuff = array("foo", "bar", "123");
//cycle through all array elements
foreach ($stuff as $singleelement)
{
//fetch! Good dog!
$result.=mysql_query("select * from databasetable where row1=$singleelement");
// .= will put everyting in the string $result, while = would just overwrite the last query
}
(or something like that, check the syntax, my php is a little rusty ;-)
the above code is not working. Please any body solve my problem.[/QUOTE]
Gods no, don't do that! That will execute a query for every row in the array -- horribly inefficient.
Rather, try:
function quote_mysql_string($s) {
return '\'' . mysql_real_escape_string($s) . '\'';
}
$result = mysql_query(sprintf('SELECT * FROM databasetable WHERE row1 in (%s)', implode(', ', array_map('quote_mysql_string', $array))));
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.