What steps can be taken to troubleshoot and resolve the "Call to undefined function mysql_connect()" error in PHP scripts?
The "Call to undefined function mysql_connect()" error occurs when the MySQL extension is not enabled in PHP or when it is deprecated. To resolve this issue, you can switch to using the MySQLi or PDO extension for database connectivity.
// Example code snippet using MySQLi extension to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";