In what scenarios should backticks be used for field names in SQL queries when working with PHP and MySQL databases?

When working with PHP and MySQL databases, backticks should be used for field names in SQL queries when the field names contain special characters, spaces, or reserved words. This is necessary to avoid syntax errors and ensure the query runs smoothly. By enclosing field names in backticks, you can ensure that the query is properly interpreted by the database engine.

// Example SQL query with backticks for field names
$field1 = 'name';
$field2 = 'age';

$sql = "SELECT `{$field1}`, `{$field2}` FROM `users`";
$result = mysqli_query($connection, $sql);

// Process the query result
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['name'] . ' is ' . $row['age'] . ' years old';
}