What are the dangers of using the mysql_ functions in PHP and what are the recommended alternatives?

The mysql_ functions in PHP are deprecated and pose security risks such as SQL injection attacks. It is recommended to use MySQLi (MySQL Improved) or PDO (PHP Data Objects) extensions, which provide better security features and support for prepared statements.

// Using MySQLi extension
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

// Using PDO extension
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);