PHP MySQL 绑定变量与 >1 条件的问题


PHP MySQL Bound Variables issue with >1 Condition

我正在构建一个搜索,其中的选项数量可变。

但是,当搜索多个变量时,我出现错误:

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

这是代码:

//Start constructing query
$query = 'SELECT * FROM `tap_jobs` WHERE `closed`=0';
//If TITLE searched for, add to query
if (isset($_GET['title'])) {
    $keywords = urldecode($_GET['title']);
    $keywords = explode(' ', trim($keywords));
    $query.= ' AND MATCH (`title`) AGAINST ("';
    foreach($keywords as $keyword => $word) {
        $query.= ' +' . $word . '';
    }
    $query.= '" IN BOOLEAN MODE)';
}
//If LOCATION/SAL1/SAL2 searched for, add to query
if (isset($_GET['location'])) $query.= ' AND `location_id`=:location_id';
if (isset($_GET['sal1'])) $query.= ' AND `sal1`>=:sal1';
if (isset($_GET['sal2'])) $query.= ' AND `sal2`<=:sal2';
//Conclude query & prepare
$query.= ' ORDER BY `date` DESC';
$stmt = $dbh->prepare($query);
//If TITLE/LOCATION/SAL1/SAL2 searched for, bind parameters
if (isset($_GET['title'])) $stmt->bindParam(':title', $title);
if (isset($_GET['location'])) $stmt->bindParam(':location_id', intval($_GET['location']));
if (isset($_GET['sal1'])) $stmt->bindParam(':sal1', intval($_GET['sal1']));
if (isset($_GET['sal2'])) $stmt->bindParam(':sal2', intval($_GET['sal2']));
//Execute query
if ($stmt->execute()) {
    while ($job = $stmt->fetch(PDO::FETCH_ASSOC)) {
        //...
    }
}

看起来您没有title参数。但是你绑定它。