#43708 [Opn->Csd]: mysqli_stmt_affected_rows return values don't match documentation

From: Date: Tue, 09 Sep 2008 02:02:58 +0000
Subject: #43708 [Opn->Csd]: mysqli_stmt_affected_rows return values don't match documentation
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1144@lists.php.net to get a copy of this message
ID: 43708 Updated by: hradtke@php.net Reported By: mo at modejong dot com -Status: Open +Status: Closed Bug Type: Documentation problem -Operating System: WinXP +Operating System: Irrelevant PHP Version: 5.2.5 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2008-03-10 19:20:34] 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-03-10 19:15:30] 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-02-25 22:38:51] 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-02-18 15:23:45] 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 ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#1144) next »