你好我正在研究一个项目,我需要从mysql表中获取所有数据,如果电子邮件与数组中的任何电子邮件匹配。 数组是由另一个表生成的.. 目前我的代码是..
$selectfrnds=mysql_query("select sender_id,receiver_id from fk_friends where (sender_id='$email' or receiver_id='$email') and friendtofriend='freq' ");
while($myallfriends=mysql_fetch_array($selectfrnds)){
$friendis=$myallfriends['sender_id'];
$detailsoffriend=mysql_query("select * from fk_views where (email='$friendis' or onprf_of='$friendis') and (views !='' or uploadpic !='') order by id asc ");
while($selecteddatais=mysql_fetch_array($detailsoffriend)){
//displaying the posts here ...
}}
这里的问题是这个代码导致分组,即它获取一封电子邮件的所有帖子,然后是另一封电子邮件的所有帖子,依此类推。但我想在数据库中得到它,我不想分组..
$ email是会话中的电子邮件。
任何帮助,请.....
答案 0 :(得分:0)
尝试在一个查询中获取它,而不是一遍又一遍地访问数据库。您的查询可能看起来像
SELECT v.*
FROM fk_views v JOIN fk_friends f
ON (v.email = f.sender_id OR v.onprf_of = f.sender_id)
WHERE (f.sender_id = '$email' OR f.receiver_id = '$email')
AND f.friendtofriend = 'freq'
AND (v.views !='' OR v.uploadpic !='')
-- GROUP BY v.id -- it's unclear without seeing your schema and sample data but you might need GROUP BY to eliminate duplicates
ORDER BY v.id