简体   繁体   English

大SQL查询-最佳做法

[英]big sql query - best practice

my php script save list of userid in a SESSION ARRAY variable for the purpose of creating html link on that page. 我的php脚本将用户ID列表保存在SESSION ARRAY变量中,目的是在该页面上创建html链接。

when we click that html link another PHP script should display all user information for that userid in a html table. 当我们单击该html链接时,另一个PHP脚本应在html表中显示该userid的所有用户信息。

I need yo know best practice to create SELECT query for this 我需要您知道最佳实践来为此创建SELECT查询

1) SELECT * from `users` WHERE  
`user_id` = '$array_valuer1' OR 
`user_id` = '$array_valuer2' OR 
`user_id` = '$array_valuer3' OR 
`user_id` = '$array_valuer4' OR 
`user_id` = '$array_valuer5' OR 
`user_id` = '$array_valuer6' OR 
----
----
`user_id` = '$array_valuern';

2) select all user --> SELECT * from `users`
   then using php script display only the userid in the SESSION array and skip other userid

which is best practice? 哪个是最佳做法? the SESSION array is temporary and i do not to want to save in a database. SESSION数组是临时的,我不想保存在数据库中。

thanks 谢谢

If you are storing few user ids in a session its okay. 如果您在会话中存储少量用户ID,则可以。 But if the number is huge dont store them in SESSION. 但是,如果数量巨大,请不要将其存储在SESSION中。 Its better to fetch them using a query. 最好使用查询来获取它们。

About the query you can use following form. 关于查询,您可以使用以下表格。

$q = "SELECT * from `users` WHERE `user_id` IN ('".implode("','", $array)."')";

shiplu.mokadd.im answer is good, but using PDO is better: shiplu.mokadd.im的答案很好,但是使用PDO更好:

$db = new PDO("...");
$statement = $db->prepare("SELECT * from `users` WHERE `user_id` IN (':list')");
$statement->execute(array(':list' => implode("','", $array)));
$row = $statement->fetch();

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

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