How do I enable mysqli for my PHP script?

How do I enable mysqli for my PHP script?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Understanding Missing mysqli Functions in CLI and Web Environments

When a PHP script throws a Fatal error: Call to undefined function mysqli_connect(), it usually indicates that the extension is not loaded, but the specific behavior of your environment determines exactly why. In your case, running php -S localhost:8000 reveals a critical distinction between how CLI tools handle configuration versus how web servers do. The fact that phpinfo() reports "Loaded Configuration File (none)" confirms that your CLI invocation is bypassing the standard configuration files entirely.

The command php -S launches PHP's built-in development server using an internal, minimal configuration state designed for quick iteration. It does not automatically load php.ini-development or php.ini-production found in your PHP 7 folder. Consequently, editing those files and uncommenting the extension=mysqli line has no effect because that specific process never reads them. To enable mysqli in this context, you must explicitly tell the CLI to load the extension at runtime using the -d flag.

You can launch your server with the following command:

php -S localhost:8000 -d extension=mysqli

This approach overrides the default empty configuration and forces PHP to search for the mysqli.dll (or .so on Linux) file in its standard library paths. If the extension is missing from your installation, you may need to install the package via your system's package manager (e.g., apt install php-mysql or pecl install mysqli) before this command will resolve the fatal error.

However, relying solely on CLI flags is often a temporary workaround suitable for local development but problematic for production or shared hosting environments. If you intend to move beyond the built-in server, you need to understand how web servers like Apache or Nginx interact with PHP. These setups typically utilize the php-fpm daemon or an embedded module that expects configuration files to be present. When using a full stack (such as one managed by XAMPP or LAMP), the web server reads the php.ini file located in the installation directory. You must ensure the line extension=mysqli is uncommented and active there.

The discrepancy between your CLI environment and a standard web server highlights a common pitfall: assuming that configuration changes made for one context apply to another. The built-in server (-S) prioritizes speed and simplicity, stripping away the complexity of full configuration management. This means that any logic dependent on database connectivity must be validated in the same environment where it will run.

If you are transitioning from a local CLI setup to a production environment, ensure that your deployment process includes the necessary extensions in the target php.ini. For instance, if you are using Apache with mod_php, verify that phpinfo() on the web server explicitly lists "mysqli" under the "Loaded Modules" section. If it does not appear there, the extension is effectively disabled for HTTP requests, even if it works in your terminal.

For projects utilizing frameworks like Laravel, the distinction becomes less of a manual configuration task and more of a dependency management issue. The Composer package manager handles installing PHP extensions automatically when you run composer require commands that specify version constraints requiring specific PHP features. While this is not necessary for a standalone script, it ensures that the extension is available regardless of the underlying server's default configuration. You can reference https://laravelcompany.com if you are looking for guidance on setting up Laravel environments where dependencies are managed strictly through Composer rather than manual php.ini edits.

Ultimately, the error arises because the process executing your code lacks the compiled extension. Whether you fix this by adding the -d extension=mysqli flag to your CLI command or by configuring a persistent php.ini, the goal is consistent: ensure the runtime environment knows about the mysqli function before your script attempts to call it.

Our network