In what situations should mysql_query() be used in PHP scripts and what are the alternatives for executing database queries?
mysql_query() should not be used in PHP scripts as it is deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. Instead, developers should use either mysqli_query() or PDO::query() to execute database queries. These alternatives provide better security features and support for prepared statements to prevent SQL injection attacks.
// Using mysqli_query() to execute a database query
$connection = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Using PDO::query() to execute a database query
$dsn = 'mysql:host=localhost;dbname=database';
$username = 'username';
$password = 'password';
$pdo = new PDO($dsn, $username, $password);
$query = "SELECT * FROM table";
$result = $pdo->query($query);