Current Location: Home> Latest Articles> Fixing the "mysqli_stmt::get_warnings Error: Statement Not Executed or Already Closed" Issue

Fixing the "mysqli_stmt::get_warnings Error: Statement Not Executed or Already Closed" Issue

gitbox 2025-06-17

When using the MySQLi extension for database operations, the mysqli_stmt::get_warnings function is commonly used to retrieve warning messages returned after executing a statement. It is often used for debugging SQL queries to check if the statement was executed correctly or if there are any warnings to be aware of. However, developers may encounter the following error in some cases:

Statement not executed or statement already closed

This error occurs when you attempt to call get_warnings on a statement object that has not been executed yet, or after the statement object has been closed. Below, we will analyze the causes of this error and provide appropriate solutions.

Causes of the Error

  1. Statement Not Executed: If the SQL statement has not been executed successfully before calling get_warnings, this error will occur. This is because get_warnings relies on the results of the SQL statement execution. If no action was performed or an error occurred during execution, it will not be possible to return any warning messages.

  2. Statement Already Closed: In MySQLi, the statement object (mysqli_stmt) remains active after execution until the close method is manually called. If you attempt to call get_warnings after the statement has been closed, this error will also occur.

  3. Connection Issues: In some cases, if the database connection is closed or there is a connection interruption, it may also prevent the retrieval of warning information.

Solutions

To fix this issue, follow these steps to check and correct your code:

1. Ensure the SQL Statement is Executed

First, ensure that the SQL statement has been successfully executed before calling get_warnings. If the statement has not been executed, get_warnings will not return any warnings.

$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute(); // Ensure the statement is executed
<p>// Only call get_warnings after the statement is executed<br>
if ($stmt->execute()) {<br>
$warnings = $stmt->get_warnings();<br>
while ($warning = $warnings->fetch_assoc()) {<br>
echo "Warning: " . $warning['Message'] . "\n";<br>
}<br>
} else {<br>
echo "Error executing statement.\n";<br>
}<br>

2. Ensure the Statement is Not Closed

Before calling get_warnings, make sure the statement object has not been closed. If the statement object has already been closed, no warning messages can be retrieved. You can adjust your code logic to ensure all operations are completed before closing the statement.

$stmt = $mysqli->prepare("INSERT INTO users (name, age) VALUES (?, ?)");
$stmt->bind_param("si", $name, $age);
$stmt->execute();
<p>// Ensure the statement is not closed before calling get_warnings<br>
$warnings = $stmt->get_warnings();<br>
while ($warning = $warnings->fetch_assoc()) {<br>
echo "Warning: " . $warning['Message'] . "\n";<br>
}</p>
<p>$stmt->close();  // Close the statement after all operations are completed<br>

3. Error Handling

It’s a good practice to include error handling when executing SQL statements to ensure errors are properly captured and handled when the statement execution fails. You can use mysqli_error or mysqli_errno to check if execution was successful.

$stmt = $mysqli->prepare("UPDATE users SET age = ? WHERE id = ?");
$stmt->bind_param("ii", $age, $userId);
<p>if (!$stmt->execute()) {<br>
echo "Error: " . $stmt->error;<br>
} else {<br>
// Retrieve warnings after the statement is successfully executed<br>
$warnings = $stmt->get_warnings();<br>
while ($warning = $warnings->fetch_assoc()) {<br>
echo "Warning: " . $warning['Message'] . "\n";<br>
}<br>
}<br>

4. Check Connection Status

If there is an issue with your database connection, it might prevent mysqli_stmt::get_warnings from functioning properly. It’s recommended to check if the database connection is successful before performing any database operations and ensure the connection remains valid while executing the statement.

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}
<p>$stmt = $mysqli->prepare("SELECT * FROM users");<br>
$stmt->execute();<br>
$warnings = $stmt->get_warnings();<br>
while ($warning = $warnings->fetch_assoc()) {<br>
echo "Warning: " . $warning['Message'] . "\n";<br>
}<br>

Conclusion

mysqli_stmt::get_warnings may return the "Statement not executed or already closed" error if the SQL statement was not executed successfully or the statement object has been closed before calling this function. To avoid this error, ensure that:

  1. The SQL statement has been successfully executed before calling get_warnings;

  2. The statement object is still open when calling get_warnings;

  3. All operations related to the statement are completed before closing the statement object.

By following these steps, you can effectively avoid encountering such errors and ensure the robustness and stability of your code.