ZF:参数号无效:没有参数绑定错误

mrN*_*mrN 7 zend-framework

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我跑它,我得到

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

请帮我解决这个问题

Nik*_*mov 12

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

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

if(empty($menuId))
   return 0; 
Run Code Online (Sandbox Code Playgroud)

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

if(empty($menuId))
   throw new Exception("No Menu ID Provided"); 
Run Code Online (Sandbox Code Playgroud)

您的完整功能如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)