【发布时间】:2013-05-29 22:45:10
【问题描述】:
您好,我正在创建一个带有查询的用户 ID 数组。对于另一个查询,我想从给定的表中进行选择,其中 user_id 是从我的第一个查询创建的数组中的一个。如何在 WHERE 子句中使用数组?
仅供参考:$row_interest 是数组
我的代码:
//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";
我尝试了“WHERE user_id IN $row_interest”,但它似乎不起作用。我可能做错了什么?
谢谢。
【问题讨论】:
-
如果您想在
where子句中使用数组,您需要确定它是由单独的where条件组成,还是您的数组引用单个值的列表健康)状况。在第一种情况下,您需要创建以and(或or)分隔的条件字符串:where field1=value1 and field2=value2。在第二种情况下,您只需将数组的值包含在逗号分隔的列表中:where aField in (value1, value2, value3)