Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得受影响的行MYSQLI prepared语句,PHP_Php_Mysqli_Prepared Statement_Rows Affected - Fatal编程技术网

如何获得受影响的行MYSQLI prepared语句,PHP

如何获得受影响的行MYSQLI prepared语句,PHP,php,mysqli,prepared-statement,rows-affected,Php,Mysqli,Prepared Statement,Rows Affected,当我插入并选择时,我似乎很难得到受影响的行,出于某种原因,它只返回-1?我使用的是一个数据库类,我在项目中一直使用它,它使用MYSQLI prepare语句来避免sql注入 有人知道为什么它总是返回-1吗?从我读到的内容来看,它应该能够在INSERT和SELECT上返回受影响的行 数据库类 class database { protected $_mysqli; protected $_debug; public function __construct($host,

当我插入并选择时,我似乎很难得到受影响的行,出于某种原因,它只返回-1?我使用的是一个数据库类,我在项目中一直使用它,它使用MYSQLI prepare语句来避免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;
    }
}

对于使用prepare创建的Select语句,您应该使用
$query->num\u rows()
mysqli\u stmt\u num\u rows($query)

当您执行
“Insert IGNORE”
时,Insert语句可能会给您带来错误,这可能导致
$query->infected_rows()
中出现-1

php.net上的一条评论(第二个链接)建议您使用
$query->sqlstate==“00000”
检查错误


见:

此函数仅适用于更新表的查询。要从SELECT查询中获取行数,请改用
mysqli\u stmt\u num\u rows()

以及:

“检查mysqli->infected_行是否等于-1不是确定
插入忽略成功的好方法。”“
语句。示例:仅当包含用户提供的数据的某些行与指定的唯一约束匹配时,在插入这些行时忽略重复键错误会导致
mysqli->受影响的_行返回-1值,即使插入了行也是如此。(在MySQL 5.0.85 linux和PHP5.2.9-2 windows上检查)。但是,如果语句执行成功,则
mysqli->sqlstate
不会返回任何错误。”