What are common syntax errors to look out for when using mysqli_real_escape_string in PHP?

Common syntax errors to look out for when using mysqli_real_escape_string in PHP include not establishing a connection to the database before calling the function, not properly escaping the string within the function, and not using the correct variable or column names. To avoid these errors, make sure to establish a connection to the database, properly escape the string using mysqli_real_escape_string, and use the correct variable or column names in your query.

// Establish a connection to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Escape the string before using it in a query
$name = mysqli_real_escape_string($connection, $name);

// Use the escaped string in a query
$query = "INSERT INTO users (name) VALUES ('$name')";
mysqli_query($connection, $query);

// Close the connection
mysqli_close($connection);