【发布时间】:2012-08-10 04:41:43
【问题描述】:
看到这是一个错误绑定错误(搜索 stackoverflow),但看起来我已经绑定了所有变量并且没有拼写错误......我在这里错过了明显的内容吗?
function connect () {
global $pdo;
try {
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "root");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
function getPhotos ($status = 0, $type = null, $session = null, $name = null) {
global $pdo;
try {
$sql = 'SELECT * FROM images WHERE 1=1';
//Status Filter Query
$sql .=' AND status = :status';
//Type Filter Query
if (!is_null($type)) {
$sql .=' AND type = :type';
}
// Session Filter Query
if (!is_null($session)) {
$sql .=' AND session = :session';
}
// Twitter Handle Filter Query
if (!is_null($name)) {
$sql .=' AND handle = :name';
}
//Prepare The Query
$stmt = $pdo->prepare($sql);
//Fire The Lasers
$stmt->execute(array(':status' => $status, ':type' => $type, ':session' => $session, ':name' => $name));
//Grab
return $stmt->fetchAll();
【问题讨论】:
-
您正在检查是否要添加参数,但是即使您没有添加参数,您也会将它们全部绑定。假设 $type 为 null,您没有将其包含在查询中,但您仍然尝试绑定它。