|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78932 Cannot fetch mysqli_prepare error if $statement variable reused
Submitted: 2019-12-08 21:27 UTC Modified: 2019-12-09 08:36 UTC
From: craig at craigfrancis dot co dot uk Assigned:
Status: Verified Package: MySQLi related
PHP Version: 7.3.12 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: craig at craigfrancis dot co dot uk
New email:
PHP Version: OS:


 [2019-12-08 21:27 UTC] craig at craigfrancis dot co dot uk
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:

	$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:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-09 08:26 UTC]
-Summary: mysqli_prepare oddity +Summary: Cannot fetch mysqli_prepare error if $statement variable reused -Status: Open +Status: Verified -PHP Version: 7.4.0 +PHP Version: 7.3.12
 [2019-12-09 08:26 UTC]
Also reproduces on older PHP versions.
 [2019-12-09 08:36 UTC]
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).
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Dec 03 14:01:27 2020 UTC