얘들 아 내가 여기이 작은 기능는 PDO는 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
그리고 콜론이':'세미콜론이 아님';') – Martin
@hime 매우 사실입니다 (세미콜론을 사용한 경우). 증거에 대한 오류를보십시오. 값이 대체되지 않기 때문에 MySQL은': state AND city = : city'를 받고 있습니다. 물론 다른 접두사 문자 (예 :'@ '또는 전혀 없음)를 사용할 수는 있지만 설명서에서 권장하는 것은 아닙니다. 그리고 무엇을 하든지'bindValue()'에 넘겨주는 매개 변수 식별자 (문서의'$ parameter')는 문자열에있는 것과 정확히 일치해야합니다. –