简体   繁体   中英

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.

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.

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:

{
"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

Obviously $stmt->execute() evals to FALSE so its not fetching.

$binds array contains exactly what $stmt needs to bind.

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 .

WHY OH WHY am i getting HY093? What am i missing?

Using PHP 5.5 and MySQL 5.5 with InnoDB.

So, thanks to @tadman's suggestion, i dug into PDO's source, and it seems the problem WAS INDEED the placeholder name.

According to Parser source code:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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