Web Hosting Talk







View Full Version : Arrays / PHP / SQL


NorthWest
01-24-2006, 04:43 PM
I have 2 different arrays that contain different types of information.
I want to have a query where the information in those array matches the information in the db.


SELECT *
FROM mytable
WHERE ‘$array1’ = colors and ‘$array2’ = fruits
ORDER BY fruits ASC

Will someone point me in the right direction here? I think I worded this correctly. Thanks in advance.

Korvan
01-24-2006, 04:59 PM
You probably want to make use of the following things

SQL IN ( ) operator and implode


$sql = "SELECT * FROM mytable WHERE colors IN(" .
implode(",", $array1) . ") and fruits IN(" .
implode(",", $array2) . ") ORDER BY fruits ASC;";


If your array is words make sure you surround each of the array values with single quotes. before inserting it into this function.
You could use http://us2.php.net/manual/en/function.array-walk-recursive.php to prep the array for inserting.

Burhan
01-25-2006, 02:40 AM
although you probably want "('".implode("','",$array1)."')" if you are checking for strings.

Korvan
01-25-2006, 11:40 AM
Thanks fyrestrtr, to make it easier to read I color coded it, blue for double quotes and red for single.
"('" . implode("','",$array1)."')"