What are best practices for handling SQL syntax errors in PHP applications?

When handling SQL syntax errors in PHP applications, it is important to properly sanitize user input to prevent SQL injection attacks. Additionally, using prepared statements can help mitigate syntax errors by separating SQL logic from user input. Finally, implementing error handling techniques, such as try-catch blocks, can help identify and address syntax errors in SQL queries.

try {
    $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
    $stmt->bindParam(':id', $userId, PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}