简体   繁体   English

MySQL的PHP​​查询-新闻/朋友饲料

[英]mySQL php query - news/ friends feed

I want to show a user the recent uploads from their friends. 我想向用户展示他们朋友最近上传的内容。

I have the users friends id's in an array: 我有一个数组中的用户朋友ID:

$friends

A user could have, potentially, thousands of friends. 一个用户可能有数千个朋友。

I can select the uploads from 1 of a users friends with: 我可以通过以下方式从1个用户朋友中选择上传:

$row = $mysqli->query("SELECT * FROM photos 
                       WHERE uploader_id =  ".$friend." 
                       ORDER BY date_uploaded 
                       DESC LIMIT ".$page.", 25");

But I need to find all of a users friends uploads. 但是我需要找到一个所有用户上传的朋友。 I thought about doing this in a loop iterating over the $friends array, but then I'd be potentially running thousands of mysql queries. 我曾考虑过在$friends数组中循环访问,但随后可能会运行数千个mysql查询。

How can I do this most efficiently? 我怎样才能最有效地做到这一点?

so to clarify: 所以要澄清一下:

search a 'photos' table for photos which are uploaded by specific users(friends), held in $friends variable, sort by date_uploaded and limit to x results so I can have pages 1, 2, 3 etc. 在“照片”表中搜索特定用户(朋友)上传的照片,保存在$friends变量中,按date_uploaded排序并限制为x个结果,因此我可以拥有第1、2、3页等

Use implode to join array elements with a comma and use MySQL's IN clause. 使用implode以逗号连接数组元素,并使用MySQL的IN子句。

$row = $mysqli->query("SELECT * FROM photos 
                   WHERE uploader_id IN (". implode(',' $friends) .") 
                   ORDER BY date_uploaded 
                   DESC LIMIT ".$page.", 25");

Make sure your $friends array doesn't contain any unsanitized input. 确保您的$friends数组不包含任何未过滤的输入。 As it stands right now you are open to SQL injection if $friends is dependent on user input in any way. 就目前而言,如果$friends以任何方式依赖于用户输入,则您可以进行SQL注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM