What are the drawbacks of using mysql_query in PHP for database queries, and what are the recommended alternatives?
Using mysql_query in PHP for database queries is not recommended as it is deprecated and has security vulnerabilities such as SQL injection. It is better to use prepared statements with either MySQLi or PDO to prevent these issues.
// Using prepared statements with MySQLi
$mysqli = new mysqli("localhost", "username", "password", "database");
$stmt = $mysqli->prepare("SELECT * FROM table WHERE column = ?");
$stmt->bind_param("s", $value);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Process the data
}
$stmt->close();
$mysqli->close();