PHP MYSQL语法错误或访问冲突:1064

时间:2017-01-16 07:46:28

标签: php mysql

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

$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

1 个答案:

答案 0 :(得分:2)

你应该替换:

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

with:

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

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