ZF:查询错误 SQLSTATE[HY093]
我在这个函数上遇到错误:
public function getCountCategoryByUrl($url, $root = 0)
{
$url = strtolower($url);
$select = $this->select()
->from($this->_name, array('count(*) as cnt'))
->where("LOWER(catalog_url) LIKE :url " . ($root > 0 ? " AND `type` = :type" : ""));
return $this->getAdapter()->fetchOne($select, array('url' => $url, 'type' => $root));
}
错误:
Message: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
我做错了什么?
I have error on this function:
public function getCountCategoryByUrl($url, $root = 0)
{
$url = strtolower($url);
$select = $this->select()
->from($this->_name, array('count(*) as cnt'))
->where("LOWER(catalog_url) LIKE :url " . ($root > 0 ? " AND `type` = :type" : ""));
return $this->getAdapter()->fetchOne($select, array('url' => $url, 'type' => $root));
}
Error:
Message: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您有
$root <= 0
时就会出现问题。在这种情况下,您的 SQL 语句仅包含一个标记 (:url
),而您绑定两个变量(:url
和:类型
)。您必须有条件地设置绑定参数:
编辑:我忽略了一些非常重要的事情。变量必须使用与 SQL 语句中定义的相同标记进行绑定。这意味着您必须使用
:url
和:type
作为绑定变量(而不是url
和type
) 。The problem arises when you have
$root <= 0
. In this case your SQL statement contains only one token (:url
) while you're binding two variables (:url
and:type
).You have to set the bound parameters conditionally:
EDIT: I've overlooked something very important. Variables must be bound with the same token as defined in the SQL statement. This means that you have to use
:url
and:type
for the bound variables (noturl
andtype
).否则,您可以执行
die($select)
来查看生成了哪些SQL语句。Else, you can do
die($select)
to view what SQL statement is generated.