What are the potential security risks of using the mysql_* functions in PHP for database queries?

Using the mysql_* functions in PHP for database queries can pose security risks such as SQL injection attacks due to the lack of prepared statements and parameterized queries. To mitigate this risk, it is recommended to switch to using MySQLi or PDO extensions which support prepared statements and bound parameters.

// Using MySQLi extension for secure database queries
$mysqli = new mysqli("localhost", "username", "password", "database");

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

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

// Set parameters and execute
$username = "example";
$stmt->execute();

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

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

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