What are the advantages of switching to PDO or mysqli functions instead of using the deprecated mysql functions in PHP?
The advantages of switching to PDO or mysqli functions instead of using the deprecated mysql functions in PHP include improved security, support for prepared statements, and better performance. PDO and mysqli offer more advanced features and better compatibility with newer versions of PHP.
// Using PDO to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}