Fatal error: Call to undefined function mysql_connect() in C:\Apache\htdocs\test.php on line 2
When you encounter Fatal error: Call to undefined function mysql_connect(), your immediate reaction might be to suspect a broken installation or a missing configuration file. However, this specific error message is almost always a symptom of a much larger architectural shift that has already occurred in the PHP ecosystem. The stack trace points directly to a deprecated extension that was removed from the core distribution years ago.
The user report describes an environment running MySQL 5.1, Apache 2.2.14, and PHP 5.2.17. While these versions align with older documentation standards like "PHP and MySQL," relying on them in a modern context is a recipe for instability. The mysql_* functions were deprecated in PHP 5.5.0 and completely removed in PHP 7.0.0. If your server is running any version of PHP newer than 5.4, the mysql_connect function will simply not exist, regardless of whether you have installed the legacy extension or configured Apache correctly.
The error occurs because the PHP interpreter cannot find the definition for mysql_connect(). In modern PHP versions, this function was stripped out entirely to enforce a transition toward more secure database handling protocols. The older API lacked support for prepared statements, which are critical for preventing SQL injection attacks, and it suffered from buffer overflow vulnerabilities. Consequently, the core development team removed it to force developers to adopt safer alternatives.
To resolve this error on a modern system, you must stop using the mysql_* functions immediately. The recommended migration path is to switch to either PDO (PHP Data Objects) or the mysqli extension. Both of these libraries support prepared statements and are actively maintained.
Here is how you would refactor the failing code snippet provided in the source material:
// DO NOT USE THIS
<?php
mysql_connect('localhost', 'root', 'password');
?>
Instead, use PDO with a DSN string to establish a connection. This approach offers better error handling and flexibility across different database drivers:
<?php
$host = 'localhost';
$db = 'database_name';
$user = 'root';
$pass = 'password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
If you are maintaining legacy code and cannot immediately rewrite the entire application, you might consider installing a backport of the old mysql extension. However, this is generally discouraged because it keeps you tied to insecure practices and prevents you from using modern PHP features. You can find such extensions via PECL, but doing so defeats the purpose of upgrading your runtime environment.
For developers looking to streamline database interactions without writing verbose PDO code, consider leveraging an Object-Relational Mapper (ORM) like Eloquent within a Laravel application. This approach abstracts the connection logic and query building entirely, allowing you to focus on business logic rather than low-level API differences. For more details on modern PHP development workflows and best practices, see https://laravelcompany.com.
Ultimately, treating this error as a sign that your application needs a migration strategy is the correct technical response. Continuing to run code dependent on removed functions guarantees failure as soon as your PHP version is updated or if you attempt to deploy to a standard shared hosting environment that no longer supports legacy extensions.