YouTip LogoYouTip

Func Mysqli Error

# PHP mysqli_error() Function The `mysqli_error()` function is a built-in PHP function used to retrieve the error description string from the most recently executed MySQLi function call. It is an essential tool for debugging database queries and connection issues during development. --- ## Definition and Usage The `mysqli_error()` function returns a string description of the last error associated with the specified database connection. If the last query or database operation succeeded without any errors, this function returns an empty string (`""`). --- ## Syntax This function can be used in both **procedural** and **object-oriented** styles. ### Procedural Style ```php mysqli_error(connection); ``` ### Object-Oriented Style ```php $mysqli->error; ``` ### Parameter Values | Parameter | Type | Description | | :--- | :--- | :--- | | `connection` | `mysqli` | **Required** (for procedural style). Specifies the MySQL connection identifier returned by `mysqli_connect()`. | --- ## Technical Details | Feature | Description | | :--- | :--- | | **Return Value** | Returns a string containing the error message. If no error occurred, it returns an empty string (`""`). | | **PHP Version** | PHP 5, PHP 7, PHP 8+ | | **Changelog** | In PHP 8.1.0+, the default error reporting mode for MySQLi has been changed from silent to throwing exceptions (`MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT`). If exceptions are enabled, you may need to wrap your code in `try-catch` blocks instead of manually checking `mysqli_error()`. | --- ## Code Examples ### Example 1: Procedural Style The following example demonstrates how to use `mysqli_error()` to catch and display errors when an `INSERT` query fails (e.g., due to a missing table or syntax error). ```php ``` ### Example 2: Object-Oriented Style The object-oriented approach accesses the error message via the `$mysqli->error` property. ```php connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } // Perform a query with an error $sql = "SELECT * FROM non_existent_table"; if (!$mysqli->query($sql)) { // Output the error description echo "Error description: " . $mysqli->error; } // Close the connection $mysqli->close(); ?> ``` --- ## Important Considerations 1. **Connection Errors vs. Query Errors**: * `mysqli_error()` requires an active connection link and is used to retrieve errors from queries or other connection-specific operations. * To catch errors that occur *during* the initial connection attempt (where a connection link has not yet been established), you must use `mysqli_connect_error()` instead. 2. **Security Best Practices**: * Never display raw database errors returned by `mysqli_error()` directly to end-users in a production environment. Revealing table names, column names, or syntax details can expose your application to security vulnerabilities. * Instead, log the detailed error message to a secure server log file and display a user-friendly generic message to the client. 3. **PHP 8.1+ Exception Handling**: * Since PHP 8.1, MySQLi throws exceptions by default. If you prefer to handle errors manually using `mysqli_error()`, you can disable exceptions by adding the following line before establishing your connection: ```php mysqli_report(MYSQLI_REPORT_OFF); ```
← Func Mysqli Fetch AllFunc Mysqli Error List β†’