What are the potential security risks of using outdated MySQL functions like mysql_query in PHP?

Using outdated MySQL functions like mysql_query in PHP can pose security risks such as SQL injection attacks, as these functions do not support parameterized queries. To mitigate this risk, it is recommended to use the improved MySQLi or PDO extensions in PHP, which support prepared statements and bound parameters for secure database interactions.

// Connect to MySQL using MySQLi
$mysqli = new mysqli("localhost", "username", "password", "database");

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

// Prepare a statement with bound parameters
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

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

// Get the result set
$result = $stmt->get_result();

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

// Close the statement and connection
$stmt->close();
$mysqli->close();