|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-12-08 21:27 UTC] 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:
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 07:00:02 2025 UTC |
Although unexpected, this is the correct behaviour. As nikic explained the destructor of mysqli_stmt is called once a new value is assigned to the same variable. The destructor performs a close operation on the MySQL server. Each time a command is sent, the error message is reset. You would need to check the error message before the mysqli_stmt is closed. Your code example would be equivalent to the following: if (false === ($statement1 = mysqli_prepare($link, 'SELECT 1'))) { exit(mysqli_errno($link) . ': ' . mysqli_error($link)); } if (false === ($statement2 = mysqli_prepare($link, 'SELECT 1 FROM this_table_does_not_exist'))) { exit(mysqli_errno($link) . ': ' . mysqli_error($link)); } if (false === mysqli_stmt_close($statement1)) { exit(mysqli_errno($link) . ': ' . mysqli_error($link)); } if (false === mysqli_stmt_close($statement2)) { exit(mysqli_errno($link) . ': ' . mysqli_error($link)); } However, the recommended practice would be to enable automatic error reporting and stop worrying about manual error checking. With automatic error reporting an exception is triggered as soon as the error happens. To enable automatic error reporting just add the following line before making a connection. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);