How can transitioning from mysql_ to MySQLi or PDO improve the overall security and functionality of a PHP application?

Transitioning from mysql_ to MySQLi or PDO can improve the overall security and functionality of a PHP application by providing prepared statements and parameterized queries, which help prevent SQL injection attacks. Additionally, MySQLi and PDO offer support for transactions, stored procedures, and more advanced features for interacting with databases, leading to more efficient and secure database operations.

// MySQLi example
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

$username = "example_user";
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    // Process the retrieved data
}

$stmt->close();
$mysqli->close();