为什么这个PDO没有执行?
$statement = $db->prepare("select * from quest where id = :'"+$testno+"'");
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo "Q:".$row["questions"];
" Q"没有回应。
答案 0 :(得分:4)
您应该将命名占位符放在准备好的sql中,并在以后将其绑定到所需的值:
$statement = $db->prepare("select * from quest where id = :id");
$statement->bindParam(':id', $testno, \PDO::PARAM_INT);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo "Q:".$row["questions"];
假设id为整数,如果不是 - 将\PDO::PARAM_INT
替换为\PDO::PARAM_STR
。
答案 1 :(得分:1)
$statement = $db->prepare("select * from quest where id = :testno");
$statement->execute(array(':testno'=>$testno));
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo "Q:".$row["questions"];
答案 2 :(得分:0)
@ X10nD,只需使用如下代码......:)
$query = "SELECT * FROM quest WHERE id = ?";
$statement = $db->prepare($query);
$statement->bindParam(1,$testno);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo "Q:".$row['questions'];