Why is it recommended to avoid using mysql_* functions in PHP and switch to other alternatives?
Using mysql_* functions in PHP is discouraged because they are deprecated and no longer maintained. It is recommended to switch to alternatives like MySQLi or PDO to ensure better security, performance, and compatibility with newer versions of PHP.
// Using MySQLi
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Using PDO
$dsn = 'mysql:host=localhost;dbname=database';
$username = 'username';
$password = 'password';
try {
$pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}