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.
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.
Are you looking for
foreach (array_expression as $value)
foreach (array_expression as $key => $value)
?
(or something like that, check the syntax, my php is a little rusty ;-)Code:$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 }
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:Code: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))));
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks