Bug #78932 [Ver]: Cannot fetch mysqli_prepare error if $statement variable reused

From: Date: Mon, 09 Dec 2019 08:36:55 +0000
Subject: Bug #78932 [Ver]: Cannot fetch mysqli_prepare error if $statement variable reused
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224154@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78932&edit=1 ID: 78932 Updated by: nikic@php.net Reported by: craig at craigfrancis dot co dot uk Summary: Cannot fetch mysqli_prepare error if $statement variable reused Status: Verified Type: Bug Package: MySQLi related PHP Version: 7.3.12 Block user comment: N Private report: N New Comment: The problem here is that the original $statement gets destroyed when the next assignment to the variable happens (that is, after the second prepare has finished). When the old $statement is destroyed a close_on_server operation on the statement is issued. This is going to reset the error state, because it performs a number of operations that may in themselves fail (like exhausting the result and closing the statement), and the error result from those operations will be used. As they don't fail, the error ends up being zero. I don't really know what we should be doing about this. I guess one possibility is to back up the error information before we do an *implicit* close, as opposed to an explicit close with mysqli_stmt_close() (in which case we *do* want to report errors from that operation). Previous Comments: ------------------------------------------------------------------------ [2019-12-09 08:26:16] nikic@php.net Also reproduces on older PHP versions. ------------------------------------------------------------------------ [2019-12-08 21:27:49] craig at craigfrancis dot co dot uk Description: ------------ When running the following test script, the error is not correctly shown. But if you un-commented the $statement = false line, then it works. It's as though the $statement variable is not being properly replaced. Test script: --------------- <?php $link = mysqli_connect('localhost', 'username', 'password', 'database'); $statement = mysqli_prepare($link, 'SELECT 1'); // $statement = false; $statement = mysqli_prepare($link, 'SELECT 1 FROM this_table_does_not_exist'); if (!$statement) { exit(mysqli_errno($link) . ': ' . mysqli_error($link)); } ?> Expected result: ---------------- 1146: Table 'database.this_table_does_not_exist' doesn't exist Actual result: -------------- 0: ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78932&edit=1

« previous php.bugs (#224154) next »