How can the use of mysqli_* or PDO functions in PHP improve the security and stability of a web application?

Using mysqli_* or PDO functions in PHP improves the security and stability of a web application by providing prepared statements that help prevent SQL injection attacks. These functions also offer better error handling, data type support, and support for multiple database systems, enhancing the overall reliability of the application.

// Using mysqli_* functions
$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);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    // Process the results
}

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