MYSQL语法错误或访问冲突

MYSQL语法错误或访问冲突

以下是我的代码,从angularjs POST请求传递的$data[0]->persinId

$idPerson=$data[0]->persinId;
$StmtQuestionType=connect_db()->query('SELECT a.`question_id` FROM answer a WHERE a.`person_id`=:PersonId');
$StmtQuestionType->bindValue(':PersonId',$idPerson);
$StmtQuestionType->execute();


但我越来越关注

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':PersonId' at line 1' in C:\xampp\htdocs\survey\insert.php:11 Stack trace: #0 C:\xampp\htdocs\survey\insert.php(11): PDO->query('SELECT a.`quest...') #1 {main} thrown in C:\xampp\htdocs\survey\insert.php on line 11

最佳答案

您应该替换:

$StmtQuestionType=connect_db()->query('SELECT a.`question_id`
                                FROM answer a WHERE a.`person_id`=:PersonId');


与:

$StmtQuestionType=connect_db()->prepare('SELECT a.`question_id`
                                FROM answer a WHERE a.`person_id`=:PersonId');


您需要首先“准备”,而不是“查询”,因为您正在使用带有命名占位符的准备好的语句。

关于php - PHP MYSQL语法错误或访问冲突:1064,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41671629/

10-09 19:30