What are the advantages of using PDO or mysqli functions over the mysql_query function in PHP?
Using PDO or mysqli functions over the mysql_query function in PHP is advantageous because PDO and mysqli offer better security through prepared statements, support for multiple database types, and object-oriented interfaces. Additionally, PDO and mysqli are more robust and provide better error handling capabilities compared to the mysql_query function.
// Using PDO to execute a prepared statement
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$results = $stmt->fetchAll();