What are the potential security risks of using outdated PHP functions like mysql_query in a database operation script?

Using outdated PHP functions like mysql_query in a database operation script can pose security risks such as SQL injection attacks. It is recommended to use modern, secure functions like mysqli or PDO with prepared statements to prevent these vulnerabilities.

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

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

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

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

// Bind the result variables
$stmt->bind_result($result);

// Fetch the results
$stmt->fetch();

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