php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #43708 mysqli_stmt_affected_rows return values don't match documentation
Submitted: 2007-12-29 21:32 UTC Modified: 2008-09-09 02:02 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mo at modejong dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.5 OS: Irrelevant
Private report: No CVE-ID: None
 [2007-12-29 21:32 UTC] mo at modejong dot com
Description:
------------
The mysqli_stmt_affected_rows implementation does not
match the documentation for this function WRT return
values when no rows are matched or when a SQL error
is found. If you run the source code below, it should
output:

1 (a)
2 (b)
mysqli_stmt_affected_rows(): int(1)
1 (b)
2 (b)
mysqli_stmt_affected_rows(): int(2)
1 (c)
2 (c)
mysqli_stmt_affected_rows(): int(0)
mysqli_stmt_affected_rows(): int(-1)

When run in PHP 5.2.4, the final two lines are:

mysqli_stmt_affected_rows(): int(-1)
mysqli_stmt_affected_rows(): NULL

The docs explicitly state that 0 will be returned
when no rows match and -1 will be returned when
an SQL error is found. The PHP impl should match
the documentation.

Reproduce code:
---------------
<?php

$link = mysqli_connect();
mysqli_select_db($link, "test");

  $query = "DROP TABLE test";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);

  $query = "CREATE TABLE test (id INTEGER, data VARCHAR(255))";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);

  $query = "DELETE FROM test WHERE id=1";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);

  $query = "DELETE FROM test WHERE id=2";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);

  $query = "INSERT INTO test VALUES (2, 'b')";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);

  $query = "INSERT INTO test VALUES (1, 'a')";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);



// Util to query and print the rows in the test table

function query() {
  global $link;

  $query = "SELECT * FROM test";
  $stmt = mysqli_prepare($link, $query);
  mysqli_stmt_execute($stmt);

  mysqli_stmt_bind_result($stmt, $id, $data);
  while (mysqli_stmt_fetch($stmt)) {
    echo "$id ($data)\n";
  }

  return $stmt;
}

// Query the data

query();

// Update data in one row

$query = "UPDATE test SET data='b' WHERE id=1";
$stmt = mysqli_prepare($link, $query);

mysqli_stmt_execute($stmt);

echo "mysqli_stmt_affected_rows(): ";
var_dump(mysqli_stmt_affected_rows($stmt));

// Query the data

query();

// Update data in two rows

$query = "UPDATE test SET data='c' WHERE data='b'";
$stmt = mysqli_prepare($link, $query);

mysqli_stmt_execute($stmt);

echo "mysqli_stmt_affected_rows(): ";
var_dump(mysqli_stmt_affected_rows($stmt));

// Query the data

$stmt = query();

// Invoking mysqli_stmt_affected_rows after a
// SELECT statement should return zero affected rows.

echo "mysqli_stmt_affected_rows(): ";
var_dump(mysqli_stmt_affected_rows($stmt));

// Invoking mysqli_stmt_affected_rows after a
// SQL error should return -1.

error_reporting(0);
$query = "SELECT * from table_that_does_not_exist";
$stmt = mysqli_prepare($link, $query);

mysqli_stmt_execute($stmt);

echo "mysqli_stmt_affected_rows(): ";
var_dump(mysqli_stmt_affected_rows($stmt));

mysqli_close($link);
?>

Expected result:
----------------
See description

Actual result:
--------------
See description

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-18 15:23 UTC] uw@php.net
What MySQL version are you using and what version of the libmysql (I assume you do not use mysqlnd, do you) do you use? 

I'm asking because of http://bugs.mysql.com/bug.php?id=23383 . 

Ulf
 [2008-02-25 22:38 UTC] mo at modejong dot com
The server version is reported as:

5.0.45-community-nt-log

I don't know how to determine what the version of
libmysql.dll is for this install. I got it as part
of xampplite 1.6.4.
 [2008-03-10 19:15 UTC] will dot fitch at gmail dot com
The reason it is returning NULL is because the check never gets beyond the "zend_parse_method_parameters".  It is expecting an object, and a boolean false is provided, therefore returning NULL.  

I would personally suggest issuing a warning and then returning NULL within the function itself. Finally, update the documentation to reflect this.
 [2008-03-10 19:20 UTC] will dot fitch at gmail dot com
Scratch that, it's doing it's job and issuing the error.  I just noticed the error_reporting.  

I really think updating the documentation to reflect that NULL will be returned if an invalid argument is supplied, although typically this can be assumed.
 [2008-09-09 02:02 UTC] hradtke@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 13:01:33 2025 UTC