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

The potential security risks associated with using the mysql_connect function in PHP for database connection include vulnerability to SQL injection attacks and lack of encryption for data transmission. To mitigate these risks, it is recommended to use parameterized queries and switch to using the mysqli or PDO extension for database connectivity.

// Using mysqli extension for secure database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";