What are the potential pitfalls of using the mysql_query function in PHP for executing SQL queries?

The potential pitfalls of using the mysql_query function in PHP include vulnerability to SQL injection attacks and deprecated functionality. To mitigate these risks, it is recommended to use parameterized queries with prepared statements in mysqli or PDO instead.

// Using parameterized queries with prepared statements in mysqli
$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Process the data here
}
$stmt->close();
$conn->close();