#43708 [Opn]: mysqli_stmt_affected_rows return values don't match documentation
ID: 43708
Updated by: uw@php.net
Reported By: mo at modejong dot com
Status: Open
Bug Type: MySQLi related
Operating System: WinXP
PHP Version: 5.2.5
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2007-12-29 21:32:26] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43708&edit=1
Thread (6 messages)