Fatal error: Call to undefined function mysql_connect()
You are running into a classic PHP deprecation issue that often confuses developers new to modern web stacks. The error message Fatal error: Call to undefined function mysql_connect() is a direct indicator that your code is trying to use the legacy MySQL extension, which has been removed from standard PHP distributions years ago. This wasn't a configuration mistake; it was a deliberate removal by the PHP core team because the API was insecure and unmaintained.
The original mysql_* functions, including mysql_connect(), mysql_query(), and mysql_fetch_assoc(), were deprecated in PHP 5.5.0 and completely removed in PHP 7.0.0. If you are seeing this error on a fresh installation of PHP 7 or higher, your application code is likely still pointing to the old API. This often happens when copying legacy scripts from older hosting environments or cloning repositories that haven't been updated for modern frameworks.
To resolve this, you need to migrate your database connection logic to the mysqli (MySQL Improved) extension or, preferably, PDO (PHP Data Objects). Both of these are active extensions included in standard PHP distributions. The migration isn't just about swapping function names; it requires a fundamental shift in how you handle prepared statements and error reporting.
Here is a practical example of what your old code might look like:
<?php
$connection = mysql_connect('localhost', 'root', 'password');
if (!$connection) {
die('Connect Error: ' . mysql_error());
}
$query = "SELECT * FROM users WHERE id = $id";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
mysql_close($connection);
?>
This code will fail immediately on modern PHP. To fix it using mysqli, you should switch to object-oriented syntax, which is generally more readable and easier to maintain:
<?php
$mysqli = new mysqli('localhost', 'root', 'password', 'database_name');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$stmt = $mysqli->prepare("SELECT * FROM users WHERE id = ?");
$id = $_GET['id']; // In production, sanitize this input properly
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
$mysqli->close();
?>
While mysqli is a good upgrade path, PDO offers more flexibility if you plan to switch database engines later. PDO allows a single connection function to work with MySQL, PostgreSQL, SQLite, and others. The syntax changes slightly:
<?php
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'root', 'password', $options);
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->execute([':id' => $id]);
$row = $stmt->fetch();
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
?>
The trade-off here is that PDO introduces an additional layer of abstraction. If you are strictly working with MySQL and want the most performant native interface, mysqli might be slightly faster in specific micro-benchmarks, but the difference is negligible for most applications. The real benefit of moving away from the old mysql_* functions is security. The legacy extension lacked support for prepared statements until very late in its lifecycle, making it highly susceptible to SQL injection attacks. Modern extensions enforce best practices or make them the default behavior.
If you are building a new application from scratch using Laravel, this issue disappears entirely because Laravel's Eloquent ORM and Query Builder handle database interactions abstractly and safely by default. For existing applications, refactoring is non-negotiable. You cannot simply enable an extension; the underlying code must be rewritten to match the capabilities of the current PHP environment.
