私はprepare文に渡す変数を次のようにします:$ subject。 PDOを使用して実行されます。残念ながら、それは一重引用符で渡されています。例として、私は数学を渡し、代わりに「数学」を使用しています。 bindParam、bindValueなどの他の答えを試してみましたが、それを文字列属性として指定していますが、動作させることはできません。誰もが間違っていることを知っている場合は、事前に感謝私のコードは以下のとおりです。PHPとMYSQLを使用して単一引用符で渡される変数
$query = "SELECT * FROM :subject;";
$sql = $connection->prepare($query);
$sql->bindParam(':subject', $subject);
try{
$sql->execute();
}catch(Exception $e){
echo $e;
}
、私は次のエラーを取得する:
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 ''maths'' at line 1' in D:\xampp\htdocs\acards\functions.php:18
Stack trace:
#0 D:\xampp\htdocs\acards\functions.php(18): PDOStatement->execute()
#1 D:\xampp\htdocs\acards\getMathsQuestions.php(13): Functions->getFeed('maths')
#2 {main}[]
クエリからセミコロンを削除します。 'SELECT * FROM:subject' – Zooly
私はまだ同じエラーを取得しようとしました –