This error usually indicates that you failed to connect to the MySQL database, or that you closed the connection before calling the mysqli_query() function.
Here are some possible causes of this error and corresponding solutions:
Failed to connect to the MySQL database: Please ensure that the mysqli_connect() function is correctly configured to connect to the MySQL database, and that the database server is running. For example:
$conn = mysqli_connect("localhost", "username", "password", "database"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }If you are using PHP 7 or later, consider using the `mysqli::report()` function to enable exception handling, making it easier to manage connection errors:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); try { $conn = mysqli_connect("localhost", "username", "password", "database"); } catch (mysqli_sql_exception $e) { die("Connection failed: " . $e->getMessage()); }Connection closed before executing the mysqli_query() function: This error occurs when the database connection is closed before calling the mysqli_query() function. Please ensure that you close the connection after completing all database operations; for example:
$conn = mysqli_connect("localhost", "username", "password", "database"); // 执行查询 mysqli_query($conn, "SELECT * FROM table1"); // 关闭连接 mysqli_close($conn);
If you have tried the above solutions but the issue persists, please check your MySQLi code to ensure that the connection is properly configured and that the connection has not been closed before calling the mysqli_query() function. If the problem still persists, try adding appropriate error checking when establishing the connection and use the mysqli_report() function to enable exception mode, which will make it easier to handle connection errors.