繁体   English   中英

错误:SQLSTATE[HY093]:无效的参数号:绑定变量的数量与标记的数量不匹配

[英]ERROR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

看到这是一个绑定不正确的错误(搜索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();

仅在实际将变量添加到查询时绑定变量:

//Status Filter Query
$sql .=' AND status = :status';

$vars = array(':status' => $status);

//Type Filter Query
if (!is_null($type)) {
    $sql .=' AND type = :type';
    $vars[':type'] = $type;
}

// Session Filter Query
if (!is_null($session)) {
    $sql .=' AND session = :session';
    $vars[':session'] = $session;
}

// etc.

$stmt->execute($vars);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM