$conn = new mysqli("localhost", "username", "password", "dbname"); // Check connection if ($conn->connect_errno) { echo "Failed to connect to MySQL: " . $conn->connect_error; exit(); } // Query the database if (!$result = $conn->query("SELECT * FROM non_existent_table")) { printf("Error message: %s\n", $conn->error); }
$file = fopen("non_existent_file.txt", "r"); if (!$file) { echo "Error message: " . error_get_last()["message"]; }This example uses the `error_get_last` function to retrieve the most recent error message after attempting to open a file that does not exist. Package/Library: None required, as this is a core PHP function.