What is the recommended method for escaping special characters like apostrophes in PHP when inserting data into a database?

When inserting data into a database using PHP, it is important to escape special characters like apostrophes to prevent SQL injection attacks and ensure data integrity. The recommended method for escaping special characters in PHP is to use the `mysqli_real_escape_string()` function. This function escapes special characters in a string for use in an SQL statement, making the data safe to insert into the database.

// Assuming $conn is your database connection
$data = "It's a test";
$escaped_data = mysqli_real_escape_string($conn, $data);

$sql = "INSERT INTO table_name (column_name) VALUES ('$escaped_data')";
mysqli_query($conn, $sql);