Is it recommended to use mysql_query() function in PHP for database operations, or are there better alternatives available?

Using the mysql_query() function in PHP for database operations is not recommended as it is deprecated and has security vulnerabilities like SQL injection. It is better to use prepared statements with PDO or MySQLi extensions for secure database operations.

// Using PDO for secure database operations
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Prepare a statement
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");

// Bind parameters
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

// Execute the statement
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);