How can error reporting and error handling functions like error_reporting(E_ALL), ini_set('display_errors', true), mysql_escape_string(), die(), and mysql_error() be used effectively in PHP?
Error reporting and handling functions in PHP can be used effectively to catch and handle errors in your code. By setting error_reporting(E_ALL) and ini_set('display_errors', true), you can ensure that all errors are reported and displayed, making it easier to identify and fix issues. Functions like mysql_escape_string() can be used to sanitize user input to prevent SQL injection attacks, while die() can be used to halt script execution and display a custom error message. Finally, mysql_error() can be used to retrieve error messages from MySQL queries for debugging purposes.
// Set error reporting and display errors
error_reporting(E_ALL);
ini_set('display_errors', true);
// Sanitize user input
$input = mysql_escape_string($_POST['input']);
// Perform MySQL query
$query = "SELECT * FROM users WHERE username = '$input'";
$result = mysql_query($query) or die('Error: ' . mysql_error());