如何在mysqli OOP中获取affected_rows的数量? Mysqli OOP affected_rows返回-1

时间:2013-12-13 10:33:06

标签: mysqli rows-affected

获取mysqli OOP中受影响行数的确切方法是什么。我正在使用mysqli OOP创建crud类。我得到int -1。

    $query = "SELECT * FROM `sk_courses`";
    $stmt = $this->_mysqli->prepare($query);
    $stmt->execute();
    $stmt->affected_rows ;

    var_dump($stmt->affected_rows);    // output is int -1

var_dump($ stmt)的输出是:

   object(mysqli_stmt)[7]
   public 'affected_rows' => null
   public 'insert_id' => null
   public 'num_rows' => null
   public 'param_count' => null
   public 'field_count' => null
   public 'errno' => null
   public 'error' => null
   public 'error_list' => null
   public 'sqlstate' => null
   public 'id' => null

1 个答案:

答案 0 :(得分:2)

这是理解mysqli_stmt文档的问题,其中包括以下每个函数的页面。

SELECT查询不会影响任何行。

查看[php docs for mysqli affected-rows]手册。在返回值下:

-1 indicates that the query returned an error.

然而,这确实是一个复杂的问题。

要获取行数,请尝试:

$query = "SELECT * FROM `sk_courses`";       // or
$query = "SELECT lastName, firstName, ... FROM `sk_courses`";

$stmt = $mysqli->prepare($query);
$stmt->execute();
$stmt->store_result();                      // without this line, num_rows = 0
print $stmt->num_rows;

然后,如果对结果集集感兴趣,请添加:

// bind result variables. next statement 'binds' them in the order of the select query
$stmt->bind_result($last, $first, .... );  // must have variable for EACH column

while ($stmt->fetch()) {
    printf ("%s (%s)\n", $first, $last);
}