What are the potential security risks associated with using the mysql_* functions in PHP and what are the recommended alternatives?

Using the mysql_* functions in PHP poses security risks such as SQL injection attacks due to lack of prepared statements and parameterized queries. It is recommended to switch to using mysqli or PDO extensions which support prepared statements and parameter binding for safer database interactions.

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

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

// Bind parameters and execute the statement
$stmt->bind_param("s", $username);
$stmt->execute();

// Bind results and fetch data
$stmt->bind_result($id, $name);
$stmt->fetch();

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