ZF:无效的参数号:没有绑定参数错误 [英] ZF: Invalid parameter number: no parameters were bound Error

查看:36
本文介绍了ZF:无效的参数号:没有绑定参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数来获取像这样的表中最后一个字段的值

I created a function to get the a value of last field in a table like this

private function _getLastPosition ($menuId) {
    $select = $this -> getDbTable() -> select();
    $select -> where("menu_id = ?", $menuId)
            -> order('position DESC');
    $row = $this -> getDbTable() -> fetchRow($select);
    if($row) {
        return $row -> position;
    } else {
        return 0;
    }
}

当我运行它时,我得到

消息:SQLSTATE[HY093]:无效的参数号:没有绑定参数

Message: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

请帮我解决这个问题

推荐答案

这通常意味着 $menuId 为空/NULL.在 $select 中使用它之前,请确保 $menuId 变量具有正确的值.:

It typically means that $menuId was empty/NULL. Ensure that $menuId variable has a proper value before using it in $select.:

您可以在函数的开头添加以下行,然后在 $select->where() 调用中使用 $menuId:

You could add the following line at the beginning of your function, before you use $menuId in your $select->where() call:

if(empty($menuId))
   return 0; 

如果没有提供 $menuId,这将返回 0.如果您想在这种情况下抛出错误(异常),您可以执行以下操作:

This will return 0 in case no $menuId was provided. If you want to throw an error (Exception) in such case, you could do the following:

if(empty($menuId))
   throw new Exception("No Menu ID Provided"); 

您的完整函数如下所示:

Your complete function would look like this:

private function _getLastPosition ($menuId) {
    if(empty($menuId))
       throw new Exception("No Menu ID Provided");
    $select = $this -> getDbTable() -> select();
    $select -> where("menu_id = ?", $menuId)
            -> order('position DESC');
    $row = $this -> getDbTable() -> fetchRow($select);
    if($row) {
        return $row -> position;
    } else {
        return 0;
    }
}

这篇关于ZF:无效的参数号:没有绑定参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆