SELECT COUNT上的PHP PDO HY093错误

时间:2014-10-15 16:06:17

标签: php mysql pdo

一直在研究所有类似的Q& A,但我现在无法找到解决方案,现在我已经在墙上敲了两天以上。

我正在根据用户输入构建查询,所以我不打算发布所有的mumbo jumbo。

在查询完成并准备好后,请考虑$binds此类型:key => val的关联数组以获取绑定值。

这是类函数的一部分(仅显示构建查询后的部分):

$stmt = $this->pdo->prepare($stmt);
foreach($binds as $k => $v){
    $col = explode('.',substr($k,1));//because all keys start with ':' and some end with '.key'
    $col = $col[0];
    if($col == 'year'){
        $col = 'model_year';
    }
    $stmt->bindValue($k,$v,$this->tblInfo->pdo_param($col));//tblInfo is a class
    //holding an array with column names and types for the table i'm working with
    //and a function that returns PDO::PARAM_ type based on the column type
}
if($stmt->execute()){
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
return array('binds' => $binds,$stmt,'err' => $stmt->errorInfo(),'result' => $res);

这是函数的json_encoded输出:

{
"binds": {
    ":make": "audi",
    ":model": "a4",
    ":year.1": "2013",
    ":price.1": "9990",
    ":gearbox.0": "auto",
    ":fuel.0": "diesel",
    ":mileage.0": "0",
    ":mileage.1": "350000"
},
"0": {
    "queryString": "SELECT COUNT(active) AS main FROM ads_auto WHERE active=true
 AND has_photo=true AND make=:make AND model=:model AND model_year <= :year.1 AND
price <= :price.1 AND gearbox=:gearbox.0 AND fuel=:fuel.0 AND mileage BETWEEN 
:mileage.0 AND :mileage.1"
},
"err": [
    "HY093",
    null,
    null
],
"result": null
}

AND debugDumpParams()

SQL: [245] SELECT COUNT(active) AS main FROM ads_auto WHERE active=true AND
has_photo=true AND make=:make AND model=:model AND model_year <= :year.1 AND 
price <= :price.1 AND gearbox=:gearbox.0 AND fuel=:fuel.0 AND mileage BETWEEN
:mileage.0 AND :mileage.1

Params:  8
Key: Name: [5]  :make      paramno=-1 name=[5]  ":make"      is_param=1 param_type=2
Key: Name: [6]  :model     paramno=-1 name=[6]  ":model"     is_param=1 param_type=2
Key: Name: [7]  :year.1    paramno=-1 name=[7]  ":year.1"    is_param=1 param_type=1
Key: Name: [8]  :price.1   paramno=-1 name=[8]  ":price.1"   is_param=1 param_type=1
Key: Name: [10] :gearbox.0 paramno=-1 name=[10] ":gearbox.0" is_param=1 param_type=2
Key: Name: [7]  :fuel.0    paramno=-1 name=[7]  ":fuel.0"    is_param=1 param_type=2
Key: Name: [10] :mileage.0 paramno=-1 name=[10] ":mileage.0" is_param=1 param_type=1
Key: Name: [10] :mileage.1 paramno=-1 name=[10] ":mileage.1" is_param=1 param_type=1

显然$stmt->execute()FALSE进行了劝告,因此无法获取。

$binds数组包含$stmt需要绑定的内容。

我没有使用任何引号。

我读过有关COUNT()和PDO的问题,所以我尝试了SELECT id而不是SELECT COUNT,但由于$stmt->execute()FALSE,所以没有区别。< / p>

为什么我为什么我得到了HY093?我错过了什么?

将PHP 5.5和MySQL 5.5与InnoDB一起使用。

1 个答案:

答案 0 :(得分:0)

所以,多亏了@ tadman的建议,我挖掘了PDO的源代码,看来这个问题已经取代了占位符名称。

根据Parser源代码:

BINDCHR = [:] [a-zA-Z0-9 _] +;

占位符名称可以是字母数字+下划线,因此添加&#39;。&#39; (点)是否定号。

感谢指针。现在一切都好。