From:             mo at modejong dot com
Operating system: WinXP
PHP version:      5.2.5
PHP Bug Type:     MySQLi related
Bug description:  mysqli_stmt_affected_rows return values don't match 
documentation

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

-- 
Edit bug report at http://bugs.php.net/?id=43708&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=43708&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=43708&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=43708&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=43708&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=43708&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=43708&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=43708&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=43708&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=43708&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=43708&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=43708&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=43708&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=43708&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=43708&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=43708&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=43708&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=43708&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=43708&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=43708&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=43708&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=43708&r=mysqlcfg

Reply via email to