How can I properly format a string for storage in a MySQL database using PHP?

When storing a string in a MySQL database using PHP, it is important to properly format the string to prevent SQL injection attacks and ensure data integrity. One way to do this is by using the mysqli_real_escape_string() function to escape special characters in the string before storing it in the database. This function adds backslashes to characters like quotes, which prevents them from being interpreted as part of the SQL query.

// Assuming $conn is your mysqli database connection object and $string is the string you want to store
$escaped_string = mysqli_real_escape_string($conn, $string);

// Now you can safely insert $escaped_string into your MySQL database
$query = "INSERT INTO table_name (column_name) VALUES ('$escaped_string')";
mysqli_query($conn, $query);