Why is it recommended to use mysqli_ or PDO instead of mysql_ functions in PHP when working with databases?

Using mysqli_ or PDO instead of mysql_ functions in PHP is recommended because the mysql_ functions are deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. This means that they are no longer supported and may cause compatibility issues with newer PHP versions. mysqli_ and PDO offer more secure and flexible ways to interact with databases, providing features such as prepared statements and parameterized queries to help prevent SQL injection attacks.

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

// Perform database operations here

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