9

当我插入和选择时,我似乎遇到了受影响的行,它只是出于某种原因返回 -1?我正在使用一个数据库类,我一直在我的项目中使用它,它使用 MYSQLI 准备语句来避免 SQL 注入。

有谁知道为什么它总是返回-1?根据我的阅读,它应该能够在 INSERT 和 SELECT 上返回受影响的行。

数据库类

class database {
    protected $_mysqli;
    protected $_debug;
 
    public function __construct($host, $username, $password, $database, $debug) {
        $this->_mysqli = new mysqli($host, $username, $password, $database);
        $this->_debug = (bool) $debug;
        if (mysqli_connect_errno()) {
            if ($this->_debug) {
                echo mysqli_connect_error();
                debug_print_backtrace();
            }
            return false;
        }
        return true;
    }
 
    public function q($query) {
        if ($query = $this->_mysqli->prepare($query)) {
            if (func_num_args() > 1) {
                $x = func_get_args();
                $args = array_merge(array(func_get_arg(1)),
                    array_slice($x, 2));
                $args_ref = array();
                foreach($args as $k => &$arg) {
                    $args_ref[$k] = &$arg; 
                }
                call_user_func_array(array($query, 'bind_param'), $args_ref);
            }
            $query->execute();
            
            if ($query->errno) {
              if ($this->_debug) {
                echo mysqli_error($this->_mysqli);
                debug_print_backtrace();
              }
              return false;
            }
 
            if ($query->affected_rows > -1) {
                return $query->affected_rows;
            }
            $params = array();
            $meta = $query->result_metadata();
            while ($field = $meta->fetch_field()) {
                $params[] = &$row[$field->name];
            }
            call_user_func_array(array($query, 'bind_result'), $params);
 
            $result = array();
            while ($query->fetch()) {
                $r = array();
                foreach ($row as $key => $val) {
                    $r[$key] = $val;
                }
                $result[] = $r;
            }
            $query->close(); 
            return $result;
        } else {
            if ($this->_debug) {
                echo $this->_mysqli->error;
                debug_print_backtrace();
            }
            return false;
        }
    }
 
    public function handle() {
        return $this->_mysqli;
    }
    
    public function last_insert_id()
    {
        return $this->_mysqli->insert_id;
    }

    public function found_rowss()
    {
        return $this->_mysqli->affected_rows;
    }
}
4

1 回答 1

6

对于使用 prepare 创建的 Select 语句,您应该使用$query->num_rows() or mysqli_stmt_num_rows($query)

当您执行"INSERT IGNORE"可能导致 -1 in时,插入语句可能会给您抑制错误$query->affected_rows()

php.net 上的评论(第二个链接)建议您使用它$query->sqlstate=="00000"来检查错误。


参见php.net (manual/en/mysqli-stmt.affected-rows)

"This function only works with queries which update a table. In order to get the number of rows from a SELECT query, use mysqli_stmt_num_rows() instead."

and php.net (manual/en/mysqli.affected-rows):

"Checking if mysqli->affected_rows will equal -1 or not is not a good method of determining success of "INSERT IGNORE" statements. Example: Ignoring duplicate key errors while inserting some rows containing data provided by user only if they will match specified unique constraint causes returning of -1 value by mysqli->affected_rows even if rows were inserted. (checked on MySQL 5.0.85 linux and php 5.2.9-2 windows). However mysqli->sqlstate returns no error if statement was executed successfully."

于 2011-11-23T14:49:02.277 回答