How can the use of mysql_escape_string function improve the security and reliability of data storage in MySQL tables via PHP?

Using the mysql_escape_string function in PHP helps improve the security and reliability of data storage in MySQL tables by escaping special characters in user input before sending it to the database. This prevents SQL injection attacks and ensures that the data is stored correctly without causing any syntax errors.

// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");

// Escape user input using mysql_escape_string
$user_input = mysql_escape_string($_POST['user_input']);

// Insert escaped user input into MySQL table
$query = "INSERT INTO table_name (column_name) VALUES ('$user_input')";
mysqli_query($conn, $query);