What are the potential security risks associated with using the mysql_connect function in PHP?

The potential security risks associated with using the mysql_connect function in PHP include vulnerability to SQL injection attacks and exposing sensitive database credentials. To mitigate these risks, it is recommended to use the mysqli or PDO extension which offer better security features such as prepared statements and parameterized queries.

// Using PDO instead of mysql_connect to establish a database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

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();
}