What are the potential security risks associated with using outdated MySQL APIs in PHP, and what are the recommended alternatives?

Using outdated MySQL APIs in PHP can expose your application to security risks such as SQL injection attacks and outdated encryption methods. It is recommended to switch to modern APIs such as PDO (PHP Data Objects) or MySQLi (MySQL Improved) which provide better security features and support for prepared statements to prevent SQL injection.

// Using PDO to connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}