Why is it recommended to use PDO or mysqli functions instead of mysql_query for SQL queries in PHP?

Using PDO or mysqli functions is recommended over mysql_query in PHP because mysql_query is deprecated and has been removed in PHP 7.0 and later versions. PDO and mysqli offer more secure and flexible ways to interact with databases, including support for prepared statements that help prevent SQL injection attacks. It is important to update your code to use PDO or mysqli functions to ensure compatibility and security.

// Using PDO to execute a SQL query
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM table WHERE id = :id');
$stmt->execute(['id' => 1]);
$results = $stmt->fetchAll();