0

大家好,我正在做一个项目,我需要从 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 是会话中的电子邮件。

任何帮助请......

4

1 回答 1

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
于 2013-08-11T07:03:22.153 回答