What are the best practices for ensuring the compatibility and portability of PHP code when using MySQL-specific syntax like backticks?

When using MySQL-specific syntax like backticks in PHP code, it can lead to compatibility issues with other database systems. To ensure portability, it's best to avoid using MySQL-specific syntax and stick to standard SQL syntax. If MySQL-specific syntax is necessary, consider using conditional statements to switch between different syntax based on the database system being used.

// Example of using conditional statements to handle MySQL-specific syntax
$db = new mysqli('localhost', 'username', 'password', 'database');

if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}

$query = "SELECT * FROM `table`";

if ($db->query("SELECT 1 FROM `table` LIMIT 1") === FALSE) {
    $query = "SELECT * FROM table";
}

$result = $db->query($query);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        // Process data
    }
}

$db->close();