如何使用多个 WHERE 子句将数组传递给 MySQL 查询
How to pass an array to a MySQL query using multiple WHERE clauses
我正在从 MySQL 数据库查询数据。
$tag = "category";
$sql = "SELECT tagsID FROM `tags` WHERE tag=:tag";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":tag", $tag, PDO::PARAM_INT);
$stmt->execute();
$query1 = $stmt->fetchAll(PDO::FETCH_OBJ);
第一个查询给出以下结果:
$arrTags = array(5) { [0]=> array(1) { ["tagsID"]=> string(2) "12" } [1]=> array(1) { ["tagsID"]=> string(2) "14" } [2]=> array(1) { ["tagsID"]=> string(2) "20" } [3]=> array(1) { ["tagsID"]=> string(2) "51" } [4]=> array(1) { ["tagsID"]=> string(2) "53" } }
来自另一个 table 的下一个查询基于上一个查询的 tagsID
。
我尝试根据 this post
来做到这一点
1- 使用 tagsID
创建一个数组
$arrList = array();
foreach($arrTags as $t){
array_push($arrList, $t['tagsID']);
}
输出:
$arrList = array(5) { [0]=> string(2) "12" [1]=> string(2) "14" [2]=> string(2) "20" [3]=> string(2) "51" [4]=> string(2) "53" }
2- 查询 2(实际上不起作用)
$active = 1;
$in = join(',', array_fill(0, count($arrList), '?'));
$sql = "SELECT * FROM `table` WHERE active=:active
AND postID IN ($in)
ORDER BY postID DESC";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":active", $active, PDO::PARAM_INT);
$stmt->execute($arrList);
$output = $stmt->fetchAll(PDO::FETCH_OBJ);
不幸的是,它不起作用。我做错了什么?
我收到错误
PDO: Invalid parameter number: mixed named and positional parameters
有没有可能以更好的方式做到这一点?
这是一个仅使用位置参数的解决方案:
$active = 1;
$in = join(',', array_fill(0, count($arrList), '?'));
$sql = "SELECT * FROM `table` WHERE active=?
AND postID IN ($in)
ORDER BY postID DESC";
$stmt = $pdo->prepare($sql);
// make an ordinal array for all the positional parameters
// with $active as the first element.
$params = array_values($arrList);
array_unshift($params, $active);
$stmt->execute($params);
$output = $stmt->fetchAll(PDO::FETCH_OBJ);
我正在从 MySQL 数据库查询数据。
$tag = "category";
$sql = "SELECT tagsID FROM `tags` WHERE tag=:tag";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":tag", $tag, PDO::PARAM_INT);
$stmt->execute();
$query1 = $stmt->fetchAll(PDO::FETCH_OBJ);
第一个查询给出以下结果:
$arrTags = array(5) { [0]=> array(1) { ["tagsID"]=> string(2) "12" } [1]=> array(1) { ["tagsID"]=> string(2) "14" } [2]=> array(1) { ["tagsID"]=> string(2) "20" } [3]=> array(1) { ["tagsID"]=> string(2) "51" } [4]=> array(1) { ["tagsID"]=> string(2) "53" } }
来自另一个 table 的下一个查询基于上一个查询的 tagsID
。
我尝试根据 this post
来做到这一点
1- 使用 tagsID
$arrList = array();
foreach($arrTags as $t){
array_push($arrList, $t['tagsID']);
}
输出:
$arrList = array(5) { [0]=> string(2) "12" [1]=> string(2) "14" [2]=> string(2) "20" [3]=> string(2) "51" [4]=> string(2) "53" }
2- 查询 2(实际上不起作用)
$active = 1;
$in = join(',', array_fill(0, count($arrList), '?'));
$sql = "SELECT * FROM `table` WHERE active=:active
AND postID IN ($in)
ORDER BY postID DESC";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":active", $active, PDO::PARAM_INT);
$stmt->execute($arrList);
$output = $stmt->fetchAll(PDO::FETCH_OBJ);
不幸的是,它不起作用。我做错了什么?
我收到错误
PDO: Invalid parameter number: mixed named and positional parameters
有没有可能以更好的方式做到这一点?
这是一个仅使用位置参数的解决方案:
$active = 1;
$in = join(',', array_fill(0, count($arrList), '?'));
$sql = "SELECT * FROM `table` WHERE active=?
AND postID IN ($in)
ORDER BY postID DESC";
$stmt = $pdo->prepare($sql);
// make an ordinal array for all the positional parameters
// with $active as the first element.
$params = array_values($arrList);
array_unshift($params, $active);
$stmt->execute($params);
$output = $stmt->fetchAll(PDO::FETCH_OBJ);