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

Using the mysql_connect function in PHP scripts can pose security risks such as SQL injection attacks, as it does not provide built-in protection against them. To mitigate this risk, it is recommended to use prepared statements or parameterized queries with mysqli or PDO instead.

// Using prepared statements with PDO to connect to a 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();
}