What are the potential pitfalls of using MySQL in PHP, especially in relation to deprecated versions?

One potential pitfall of using MySQL in PHP, especially with deprecated versions, is that it can lead to security vulnerabilities and compatibility issues with newer PHP versions. To solve this issue, it is recommended to upgrade to the latest version of MySQL and use prepared statements to prevent SQL injection attacks.

// Connect to MySQL database using prepared statements
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

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

// Prepare and bind SQL statement
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

// Set parameters and execute
$username = "JohnDoe";
$stmt->execute();

// Fetch results
$result = $stmt->get_result();

// Display results
while ($row = $result->fetch_assoc()) {
    echo "Username: " . $row["username"] . "<br>";
}

// Close connection
$stmt->close();
$conn->close();