How can the transition from the deprecated mysql_ functions to mysqli or PDO improve the security and reliability of PHP applications?

The transition from the deprecated mysql_ functions to mysqli or PDO can improve the security and reliability of PHP applications by providing prepared statements and parameterized queries, which help prevent SQL injection attacks. Additionally, mysqli and PDO offer better error handling and support for transactions, making the code more robust and secure.

// Using mysqli to connect to a database
$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);
}

// Perform a query using prepared statement
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

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

// Get result
$result = $stmt->get_result();

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

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