Ffile2fix
Sign in Get started

Fix "Call to undefined function" in PHP

PHP Fatal error:  Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/db.php:5

PHP was asked to run a function that does not exist at that moment. Either the function belongs to a PHP extension that is not installed, it was removed in a newer PHP version (like mysql_connect() or each()), or the file that defines it was never loaded.

Also appears as: Fatal error: Uncaught Error: Call to undefined function mb_strlen() · Call to undefined function imagecreatefromjpeg() · Fatal error: Call to undefined function curl_init() · Uncaught Error: Call to undefined function each()

Common causes

  • The function was removed from PHP, such as mysql_* in PHP 7.0 or each() and create_function() in PHP 8.0
  • A required extension is missing: mbstring, curl, gd, intl, zip or xml
  • The file that defines the function was not included, or the Composer autoloader was not loaded
  • A typo in the function name, or a namespaced function called without its namespace
  • The function is defined conditionally or inside another function that has not run yet
  • The host lists the function in disable_functions

How to fix it

  1. Identify where the function should come from. Search the PHP manual for the function name. If it belongs to an extension (curl_*, mb_*, image*, gd_*), it is an extension problem; if the manual says it was removed, the code needs updating; otherwise it should be defined in your project.
  2. Install the missing extension. On Debian/Ubuntu run sudo apt install php8.3-mbstring php8.3-curl php8.3-gd (matching your PHP version), then restart PHP-FPM or Apache. Confirm with php -m | grep -i curl, and check the web server's phpinfo() as well.
  3. Replace removed functions. Replace mysql_* with mysqli or PDO, each() with foreach, create_function() with an anonymous function, and money_format() with NumberFormatter. A plugin or theme that uses them needs an update or replacement.
  4. Load the defining file. Make sure require_once 'path/to/functions.php'; runs before the call, or that vendor/autoload.php is required. For Composer packages that ship functions, check the files section of the package's autoload config and run composer dump-autoload.
  5. Check namespaces and spelling. Inside a namespace, a function defined in another namespace must be imported with use function Vendor\Pkg\helper; or called by its full name. Also check disable_functions in phpinfo() if the name is correct.

Replacing removed mysql_* calls with PDO

// Old (removed in PHP 7.0)
// $link = mysql_connect('localhost', 'user', 'pass');

$pdo = new PDO(
    'mysql:host=localhost;dbname=app;charset=utf8mb4',
    'user',
    'pass',
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);

How to stop it happening again

  • Declare required extensions in composer.json, for example "ext-mbstring": "*"
  • Scan code for deprecated and removed functions before upgrading PHP
  • Keep development, staging and production on the same PHP version and extensions
  • Use function_exists() checks only for genuinely optional features

Frequently asked questions

Why does it work on the command line but not in the browser?

The CLI and the web server often load different php.ini files and extensions. Compare php -m with the extension list in a phpinfo() page served by the web server.

What replaced mysql_connect()?

The mysql extension was removed in PHP 7.0. Use mysqli_connect() or PDO, and switch to prepared statements while you are changing the code.

Can I just define the missing function myself?

You can write a polyfill wrapped in if (!function_exists('name')), and packages like symfony/polyfill-mbstring do this. For removed functions, rewriting the calls is usually cleaner.