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

The potential security risks of using the mysql_connect function in PHP include SQL injection attacks and exposing sensitive database credentials in the code. To mitigate these risks, it is recommended to use PDO (PHP Data Objects) or MySQLi extension which provide prepared statements to prevent SQL injection and support parameterized queries for securely passing database credentials.

// Using PDO to connect to MySQL database securely
$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();
}