我试图videos根据某些属性 ( age, year, countries)获取一些。出于某种原因,即使我正确地将参数绑定到查询并正确指定了查询 ( u.country NOT IN ($countries_count)),我仍然会得到country = U.S.A. 我的bindParam. 请帮忙。<?php $parameters = json_decode(file_get_contents('php://input'), true); $age = $parameters["age"]; $year = $parameters["year"]; $countries = sizeof($parameters["countries"]) == 0 ? array("0") : $parameters["countries"]; $countries_count = implode(",", array_fill(0, sizeof($countries), "?")); $sql = "SELECT v.title, u.name FROM video AS v JOIN user AS u ON v.id_user = u.id WHERE u.age <= ? AND YEAR(v.upload_date) >= ? AND u.country NOT IN ($countries_count);"; $connection = new PDO("mysql:host=localhost;dbname=data_base", "root", ""); $statement = $connection->prepare($sql); $statement->bindParam(1, $age, PDO::PARAM_INT); $statement->bindParam(2, $year, PDO::PARAM_INT); foreach ($countries as $k => $x) { $statement->bindParam($k+3, $x, PDO::PARAM_STR); } $statement->execute(); echo json_encode($statement->fetchAll());?>
2 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
您的问题是您将所有IN
参数绑定到同一个变量 ( $x
),因此它们最终都具有相同的值。您可以通过更改为bindValue
或绑定到实际数组值来解决这个问题,即
$statement->bindParam($k+3, $countries[$k], PDO::PARAM_STR);
素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
将 bindParam 更改为 bindValue
如果要使用bindParam,请将您的sql更改为
u.age <= :age and
YEAR(v.upload_date) >= :year
...
然后绑定参数:
->bindParam(':age', $age)
->bindParam(':year', $year)
- 2 回答
- 0 关注
- 271 浏览
添加回答
举报
0/150
提交
取消