Zend Framework插入数据库错误处理

时间:2018-09-05 18:54:55

标签: php zend-framework error-handling

我正在使用Zend_Db_Adapter_Abstract来处理DB数据,当我对没有权限向DB中插入数据的DB用户使用其方法insert时,代码500出现错误。如果此方法没有引发任何异常,我会处理这种情况吗?

方法如下:

public function insert($table, array $bind)
{
    // extract and quote col names from the array keys
    $cols = array();
    $vals = array();
    $i = 0;
    foreach ($bind as $col => $val) {
        $cols[] = $this->quoteIdentifier($col, true);
        if ($val instanceof Zend_Db_Expr) {
            $vals[] = $val->__toString();
            unset($bind[$col]);
        } else {
            if ($this->supportsParameters('positional')) {
                $vals[] = '?';
            } else {
                if ($this->supportsParameters('named')) {
                    unset($bind[$col]);
                    $bind[':col'.$i] = $val;
                    $vals[] = ':col'.$i;
                    $i++;
                } else {
                    /** @see Zend_Db_Adapter_Exception */
                    require_once 'Zend/Db/Adapter/Exception.php';
                    throw new Zend_Db_Adapter_Exception(get_class($this) ." doesn't support positional or named binding");
                }
            }
        }
    }

    // build the statement
    $sql = "INSERT INTO "
         . $this->quoteIdentifier($table, true)
         . ' (' . implode(', ', $cols) . ') '
         . 'VALUES (' . implode(', ', $vals) . ')';

    // execute the statement and return the number of affected rows
    if ($this->supportsParameters('positional')) {
        $bind = array_values($bind);
    }
    $stmt = $this->query($sql, $bind);
    $result = $stmt->rowCount();
    return $result;
}

0 个答案:

没有答案