What potential pitfalls should be avoided when using the mysql_* functions in PHP for database queries?

Using the mysql_* functions in PHP for database queries can lead to security vulnerabilities like SQL injection attacks. To avoid this, it's recommended to use parameterized queries with prepared statements or switch to using the mysqli or PDO extension instead.

// Using mysqli prepared statement to avoid SQL injection
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

$username = "example_user";
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    // Process the data
}

$stmt->close();
$mysqli->close();