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

时间:2015-01-27 09:11:02

标签: php mysql

我构建了一个可变数量的选项。

然而,当搜索多个变量时,我有错误:

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)) {
        //...
    }
}

1 个答案:

答案 0 :(得分:1)

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