一直在研究所有类似的问答环节,但是我现在无法找到解决方案,现在我已经在墙上敲了两天多了 .
我正在根据用户输入构建查询,所以我不会发布所有的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
,因此没有任何区别 .
WHY OH WHY 我有HY093吗?我错过了什么?
使用PHP 5.5和MySQL 5.5与InnoDB .
1 回答
所以,多亏了@ tadman的建议,我挖掘了PDO的源代码,似乎问题是因为占位符名称 .
根据Parser源代码:
BINDCHR = [:] [a-zA-Z0-9_];
占位符名称可以是字母数字下划线,因此添加“ . ” (点)是不是没有 .
谢谢你的指针 . 现在一切都好 .