Are there best practices for handling strings and reserved words in MySQL queries when using PHP?

When handling strings and reserved words in MySQL queries in PHP, it is important to properly escape the strings to prevent SQL injection attacks and to enclose reserved words in backticks to avoid conflicts with MySQL keywords. This can be achieved using the mysqli_real_escape_string function for strings and manually adding backticks for reserved words.

// Example of handling strings and reserved words in MySQL queries in PHP

// Connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Handle strings by escaping them
$name = mysqli_real_escape_string($mysqli, "John Doe");

// Handle reserved words by enclosing them in backticks
$column = "`order`";

// Construct and execute the query
$query = "SELECT * FROM table WHERE name = '$name' AND $column = '123'";
$result = $mysqli->query($query);

// Process the result
while ($row = $result->fetch_assoc()) {
    // Do something with the data
}

// Close the connection
$mysqli->close();