gpt4 book ai didi

PHP PDO : Can't bind value to multiple variables?

转载 作者:行者123 更新时间:2023-11-29 05:02:20 26 4
gpt4 key购买 nike

我正在尝试通过以下查询执行一个使用相同外键连接多个表的查询,但它返回 false。

$question_id = 11406;

$query = $db->prepare("SELECT q.question_attempts_permitted, q.question_range, a.answer_text, r.attempt_count
FROM checkup_questions q, checkup_answers a, user_responses r
WHERE a.question_id=:question_id AND q.question_id=:question_id AND r.question_id=:question_id");
$query->bindValue(':question_id', $question_id, PDO::PARAM_INT);
$query->execute();

但是,如果我直接注入(inject) question_id,查询将返回所需的结果。

$query = $db->prepare("SELECT q.question_attempts_permitted, q.question_range, a.answer_text, r.attempt_count 
FROM checkup_questions q, checkup_answers a, user_responses r
WHERE a.question_id=11406 AND q.question_id=11406 AND r.question_id=11406");
$query->execute();

bindValue 接口(interface)是否存在某些限制导致第一个查询失败而第二个查询按预期返回?

最佳答案

应使用JOIN 重写查询文本:

$query = $db->prepare("
SELECT q.question_attempts_permitted, q.question_range, a.answer_text, r.attempt_count
FROM checkup_questions q
JOIN checkup_answers a ON a.question_id = q.question_id
JOIN user_responses r ON r.question_id = q.question_id
WHERE q.question_id=:question_id
");
// you can provide placeholder without `:`
$query->bindValue('question_id', $question_id, PDO::PARAM_INT);
$query->execute();

这里只有一个占位符。

关于PHP PDO : Can't bind value to multiple variables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com