The error you're encountering happens because the `mysql_connect()` function is no longer supported in PHP versions 7.0 and later. This function was removed in favor of the newer `mysqli` or `PDO` extensions, which offer better security and performance. However if you still want to use mysql_connect and not the mysqli you might have to consider using older version of php like php5 and required extensions.
To resolve this issue, you can replace `mysql_connect()` with `mysqli_connect()` or refactor your code to use the `PDO` extension. Here's how you can update your code:
Using `mysqli_connect()`
<?php
// Database connection details
$servername = "your_server_name";
$username = "your_username";
$password = "your_password";
$database = "your_database_name";
// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check the connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Using `PDO`
<?php
try {
$dsn = "mysql:host=your_server_name;dbname=your_database_name";
$username = "your_username";
$password = "your_password";
// Create a PDO instance
$conn = new PDO($dsn, $username, $password);
// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Both approaches should work, depending on your preference. The `mysqli` extension is more similar to the old `mysql` extension, whereas `PDO` offers more flexibility, including support for multiple database types.
