我试图从mysql数据库中获取数据...但我只需要数组指定的结果...例如我有一个数据库,其中填充了成员数据:用户名,电子邮件等。另一张桌子上我按照会员的方式存储了他们的联系人列表......所以我正在寻找一种方式来展示他们的联系人名单...我不知道最好的方式是什么...欢迎任何提示......例如:
$result = mysql_query("select contact_id from member_contact_list");
$contacts = array();
while ($row = mysql_fetch_array($result)){
$c_array[] = $row[ 'username' ];
}
$c_array = implode( ',', $existing_users );
$sql = "SELECT * FROM members WHERE id="c_array[]"";
有点难以解释......我希望有人明白我的意思。
答案 0 :(得分:2)
答案 1 :(得分:0)
使用In
$sql = "SELECT * FROM members WHERE id in (".$c_array.")";
答案 2 :(得分:0)
使用JOIN
,您可以获得给定成员的联系人列表:
SELECT * FROM members AS m
LEFT JOIN
contacts AS c ON (m.user_id = c.user_id)
WHERE m.user_id = 1;
JOINS
比子查询更有效
答案 3 :(得分:0)
奇怪的程序。
$result = mysql_query("select contact_id from member_contact_list");
$contacts = array(); //**not used anywhere else in the given code**
while ($row = mysql_fetch_array($result)){
$c_array[] = $row[ 'username' ]; //**was that supposed to be $contacts?**
//** also was this the right version: $contacts = $row['id']; ??? **//
}
$c_array = implode( ',', $existing_users );
//** where was $existing_users defined? Was that supposed to be $contacts, too?**
$sql = "SELECT * FROM members WHERE id="c_array[]"";
//** correct version is indeed with the clause "WHERE id IN (".$c_array.")" **;
//** last but not least, the $c_array seems to be the collection of user names, not user ids. **//