What function can be used to display MySQL errors in PHP?
To display MySQL errors in PHP, you can use the mysqli_error() function. This function returns the error message from the most recent MySQL function call. By checking for errors after executing MySQL queries, you can handle them appropriately in your PHP code.
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Execute MySQL query
$result = $mysqli->query("SELECT * FROM table");
// Check for errors
if (!$result) {
echo "Error: " . $mysqli->error;
}
// Close connection
$mysqli->close();
Keywords
Related Questions
- How can one improve the flexibility of code that relies on specific array indexes, such as in the example provided?
- How can PHP developers ensure that they are using the correct timezone when working with timestamps?
- How can PHP be used to validate input in a form, including allowing for special characters like umlauts and spaces?