Hi I am creating an array of user id's with a query. With another query I would like to select from a given table where the user_id is one that is in the array created from my very first query. How can I use an array in my WHERE clause?
Just for reference: $row_interest is the array
My Code:
//Grabs the user id's of the users that have the queried interest
$interest_search_query= "SELECT DISTINCT user_id FROM interests WHERE interest LIKE
'%".$search_term."%'";
$interest_search_result= mysqli_query($connect, $interest_search_query);
$row_interest= mysqli_fetch_array($interest_search_result);
//Grabs the user information with each user id
$search_query= "SELECT DISTINCT user_id, fname, lname, profile_pic, school FROM users
WHERE user_id IN $row_interest";
I tried "WHERE user_id IN $row_interest", but it doesn't seem to work. What could I be doing wrong?
Thanks.
where
clause, you'll need to decide if it consists of separatewhere
conditions or if your array refers to a list of values in a single condition. In the first case, you'll need to create a string of conditions separated withand
(oror
):where field1=value1 and field2=value2
. In the second case, you simply need to include the values of the array in a comma-separated list:where aField in (value1, value2, value3)
– Barranka May 29 at 22:48