嘿傢伙我正在這裏這個小功能PDO返回MySQL錯誤與bindParam
function getBeaches() {
$request=Slim::getInstance()->request();
$args=filter_var_array(func_get_args(),FILTER_SANITIZE_STRING);
$sql="SELECT * FROM beaches WHERE state=:state AND city=:city";
// var_export($args); die();
// array (0 => 'wa', 1 => 'seattle',)
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindValue('state', $args[0], PDO::PARAM_STR); //should bind wa
$stmt->bindValue('city', $args[1], PDO::PARAM_STR); //should bind seattle
$stmt->execute();
$stmt = $db->query($sql);
$beaches = $stmt->fetchObject();
$db = null;
echo '{"map": ' . stripslashes(json_encode($beaches)) . '}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
/* {"error":{"text":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 ':state AND city=:city' at line 1}}
*/
}
而且我得到我在底部評論的錯誤,試圖像這樣
mysql$ SELECT * FROM beaches WHERE state='wa' AND city='seattle';
可能是這枚戒指的一些鐘聲?
這不是真的,bindParam和bindValue並且沒有冒號運行。 – ehime 2012-07-17 00:47:22
注意它們冒號':'不是分號';'; – Martin 2012-07-17 00:49:10
@ehime這是非常真實的(對於你使用分號的情況)。看看你的錯誤證明。 MySQL正在接收':state AND city =:city',因爲這些值沒有被替換。當然你可以使用另一個前綴字符(比如'@'或根本就沒有),但這不是手冊推薦的。無論你做什麼,你傳入'bindValue()'的參數標識符(docs中的'$ parameter')必須與你的字符串中的參數標識符完全匹配。 – 2012-07-17 00:49:47