我想寫使用PDO選擇使用使用PHP MySQL的其中PDO
function getRec($id=0)
{
($id==0?$addQuery="":$addQuery=" where id =".$id);
$statement = $dbh->prepare("select * from TCMS :name order by id");
$statement->execute(array(':name' => $addQuery));
$row = $statement->fetchAll();
return $row ;
}
在PHP和MySQL的函數來選擇PHP和MySQL的價值觀,我得到錯誤
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 '' where id =2' order by id' at line 1' in /Applications/XAMPP/xamppfiles/htdoc
實際上什麼我試圖
如果值(2)ID傳遞則說明會
select * from TCMS where id=2 order by id
如果ID = 0,則select語句將
select * from TCMS order by id
我是新來的PDO和不知道確切的語法。
該怎麼辦?
你不能那樣做。佔位符不是任意插入SQL的字符串,它們只能用於像'WHERE something =:placeholder'這樣的參數 –
親愛的@Michael Berkowski感謝您的回覆,那麼如何解決這個問題?我需要此 – air
的幫助,請參閱下面的答案。 –