|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-02-18 15:23 UTC] uw@php.net
[2008-02-25 22:38 UTC] mo at modejong dot com
[2008-03-10 19:15 UTC] will dot fitch at gmail dot com
[2008-03-10 19:20 UTC] will dot fitch at gmail dot com
[2008-09-09 02:02 UTC] hradtke@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 03:00:01 2025 UTC |
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