简体   繁体   English

SELECT COUNT上的PHP PDO HY093错误

[英]PHP PDO HY093 error on SELECT COUNT

Been researching all the similar Q&A around, but i can't seem to find a solution, after banging my head on the walls for over 2 days now. 一直在研究所有类似的问答,但在将头撞在墙上超过2天后,我似乎找不到解决方案。

I'm building a query based on user input, so i'm not going to post all the mumbo jumbo. 我正在基于用户输入来构建查询,所以我不会发布所有的巨型消息。

Consider $binds an associative array of this type :key => val for binding values once the query is complete and prepared. 考虑完成查询并准备好$binds这种类型的关联数组:key => val以绑定值。

This is part of a class function (showing only the part after the query is built): 这是类函数的一部分(仅显示查询生成后的部分):

$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);

This is a json_encoded output of the function: 这是该函数的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() : 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

Obviously $stmt->execute() evals to FALSE so its not fetching. 显然$stmt->execute()评估为FALSE因此无法获取。

$binds array contains exactly what $stmt needs to bind. $binds数组完全包含$stmt需要绑定的内容。

I'm not using any quotes. 我没有使用任何引号。

I read about issues with COUNT() and PDO, so i tried a SELECT id instead of SELECT COUNT , but it makes no difference since $stmt->execute() is FALSE . 我读到有关COUNT()和PDO的问题,所以我尝试使用SELECT id而不是SELECT COUNT ,但这没什么区别,因为$stmt->execute()FALSE

WHY OH WHY am i getting HY093? 为什么,为什么我会得到HY093? What am i missing? 我想念什么?

Using PHP 5.5 and MySQL 5.5 with InnoDB. 在InnoDB上使用PHP 5.5和MySQL 5.5。

So, thanks to @tadman's suggestion, i dug into PDO's source, and it seems the problem WAS INDEED the placeholder name. 因此,由于@tadman的建议,我深入研究了PDO的来源,看来该问题确实与占位符名称有关。

According to Parser source code: 根据解析器源代码:

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

Placeholder name can be alphanumeric + underscore, so adding '.' 占位符名称可以是字母数字+下划线,因此请添加“。”。 (dot) is a no no. (点)为否。

Thanks for the pointer. 感谢您的指导。 All is OK now. 现在一切都好。

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

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