【发布时间】:2016-07-07 04:48:11
【问题描述】:
对数组中的每个项目运行 SQL 查询的最佳方式是什么?
我有 $array 的代码:
Array
(
[0] => 12345
[1] => 12346
[3] => 12347
)
现在我想为 $array 中的每个项目运行以下 SQL 查询:
SELECT * FROM `TABLE` a WHERE a.`Code` = :code
我一直在使用的 PHP:
$results = array();
$statement = $pdo->prepare($sql);
$statement->bindParam(':code', $value);
foreach ($array as $key => $value) {
$statement->execute();
while (($results = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
echo $results;
}
}
【问题讨论】:
-
当 MySQL 游标被消耗时,您需要再次执行查询。据我所知,除了 MySQL 缓存之外,没有其他方法可以避免另一个查询。
-
你为什么要这样做?
-
SELECT * FROM
TABLEA WHERE a.Codein :array