Question

Find the PHP CLI php.ini File and Enable Extensions

phpeasyphp

Question

I am using EasyPHP and need to enable the pdo_mysql extension. I uncommented this line in a php.ini file:

extension=php_pdo_mysql.dll

However, the problem remains. Because I am running PHP from the command line, I suspect that the CLI may be using a different php.ini file. How can I find the php.ini file that the PHP CLI actually uses?

Short Answer

PHP can run under different environments, such as a web server and the command line. Each environment may load a different configuration file. You will learn how to identify the exact PHP executable used by your terminal, locate its loaded php.ini, verify that an extension is enabled, and diagnose common configuration problems.

Concept

PHP configuration is controlled primarily by an INI file, usually named php.ini. It contains settings such as error reporting, timezone, memory limits, and enabled extensions.

The important detail is that the PHP CLI and your web server are separate PHP runtime environments. For example:

  • Your terminal may execute one php.exe from PATH.
  • EasyPHP's Apache server may use a different php.exe and a different php.ini.
  • A command can also load additional .ini files from a configuration scan directory.

Editing a php.ini file only changes PHP if it is the file loaded by the PHP executable you are actually running. Therefore, first identify the CLI executable and its configuration, then enable the extension there.

For pdo_mysql, PHP must be able to find and load the extension file. On Windows installations, this is often a DLL such as php_pdo_mysql.dll in PHP's ext directory.

Mental Model

Think of each PHP environment as a separate kitchen.

  • The PHP executable is the chef.
  • The php.ini file is the chef's recipe book.
  • PHP extensions such as pdo_mysql are tools in the kitchen.

You may edit a recipe book in one kitchen (for example, EasyPHP's web-server PHP), while your terminal is using a chef in another kitchen with a different recipe book. php --ini asks the current command-line chef which recipe book it opened.

Take Quiz

Syntax and Examples

Use the following command in the same terminal where you run your PHP scripts:

php --ini

Typical output looks similar to this:

Configuration File (php.ini) Path: C:\EasyPHP\php
Loaded Configuration File:         C:\EasyPHP\php\php.ini
Scan for additional .ini files in: C:\EasyPHP\php\conf.d
Additional .ini files parsed:      C:\EasyPHP\php\conf.d\custom.ini

The most useful line is:

Loaded Configuration File: C:\EasyPHP\php\php.ini

That is the main php.ini used by the CLI.

To see which PHP executable your shell runs, use this on Windows:

where php

Example:

C:\EasyPHP\php\php.exe
C:\Program Files\PHP\php.exe

If more than one path appears, the first one is normally the executable selected through PATH.

To check whether pdo_mysql is loaded:

php -m

Look for both of these module names:

PDO
pdo_mysql

You can also inspect the exact configuration values:

php -i

On Windows, filter the output with findstr:

php -i | findstr /I "Loaded Configuration File extension_dir pdo_mysql"

A typical Windows extension setting is:

extension_dir = "ext"
extension=php_pdo_mysql.dll

Modern PHP versions may also accept this shorter, portable form:

extension=pdo_mysql

Step by Step Execution

Suppose you run this command:

php --ini

And receive:

Configuration File (php.ini) Path: C:\EasyPHP\php
Loaded Configuration File:         C:\EasyPHP\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Follow this sequence:

  1. php is resolved by your terminal to a particular php.exe.

  2. That executable searches for configuration according to its startup rules.

  3. PHP reports C:\EasyPHP\php\php.ini as the loaded configuration file.

  4. Open that exact file.

  5. Ensure the extension line is enabled:

    extension=php_pdo_mysql.dll
    
  6. Save the file.

  7. Start a new terminal, then run:

    php -m | findstr /I pdo_mysql
    
  8. If pdo_mysql appears, the CLI successfully loaded the extension.

The CLI reads its configuration each time a new PHP process starts. Unlike Apache or PHP-FPM, it usually does not need a service restart; opening a new terminal or running the command again is sufficient.

Real World Use Cases

  • Running database migrations: Framework migration commands need pdo_mysql to connect to MySQL from the terminal.
  • Composer scripts: A Composer command may invoke PHP CLI, so required extensions must be enabled for the CLI installation.
  • Scheduled tasks: Cron jobs or Windows Task Scheduler jobs use CLI PHP and may fail if their required extensions are missing.
  • Debugging environment differences: A website may work through Apache while a command such as php artisan migrate fails because each uses different PHP configuration.
  • Build and deployment scripts: CI pipelines often run php, and checking php --ini makes configuration failures reproducible.
Take Quiz

Real Codebase Usage

Developers commonly add environment diagnostics before changing settings:

where php
php --version
php --ini
php -m

This establishes four facts:

  1. Which executable is running.
  2. Which PHP version it is.
  3. Which main configuration file it loaded.
  4. Which extensions are available.

For an application that needs MySQL through PDO, a practical validation command is:

php -r "var_dump(extension_loaded('pdo_mysql'));"

Expected output:

bool(true)

In projects, configuration issues are often handled with a fail-fast check. For example, a setup script can stop with a clear message when an extension is absent:

<?php

if (!extension_loaded('pdo_mysql')) {
    fwrite(STDERR, "The pdo_mysql extension is required. Run php --ini to find the active configuration.\n");
    exit(1);
}

echo "pdo_mysql is available.\n";

This is more helpful than allowing a later database connection to fail with a vague driver error.

Common Mistakes

Editing the wrong php.ini

It is common to edit the php.ini found in a web-server folder while the terminal executes another PHP installation.

Avoid this by running:

php --ini
where php

Assuming php.ini-development or php.ini-production is active

PHP distributions may contain template files named php.ini-development and php.ini-production. They are not necessarily loaded.

Check Loaded Configuration File rather than editing a file based only on its name.

Leaving a semicolon at the beginning

A semicolon comments out an INI directive:

; extension=php_pdo_mysql.dll

This does not load the extension. Remove the semicolon:

extension=php_pdo_mysql.dll

Using an incorrect extension_dir

PHP must be able to locate php_pdo_mysql.dll. If the extension directory is wrong, PHP may show a startup warning.

extension_dir = "ext"
extension=php_pdo_mysql.dll

Use php -i | findstr /I extension_dir to inspect the active value.

Not checking startup warnings

Run a simple command after changing configuration:

php -v

If PHP cannot load a DLL, it often prints a warning here. Read the entire warning; it may indicate a missing file, an incompatible PHP version, or a missing dependency.

Expecting a web-server change to affect CLI

Restarting Apache can be necessary after changing Apache's PHP setup, but it does not change what a separate command-line php.exe uses. Verify the CLI independently.

Confusing PDO with pdo_mysql

PDO is the database abstraction layer. pdo_mysql is the specific driver that lets PDO communicate with MySQL. For PDO MySQL connections, both must be loaded.

Comparisons

Command or toolWhat it tells youBest use
php --iniLoaded php.ini, scan directory, and additional INI filesFinding the CLI configuration source
where php on WindowsAll matching php.exe files on PATHDetecting multiple PHP installations
php -vPHP version and startup warningsQuickly spotting extension load errors
php -mLoaded PHP module namesConfirming pdo_mysql is enabled
php -iDetailed PHP runtime configurationInspecting values such as extension_dir
phpinfo() in a browserConfiguration used by the web-server PHP runtimeComparing browser PHP with CLI PHP
EnvironmentTypical configuration checkRestart needed after editing?
PHP CLIphp --iniNo; run PHP again
Apache modulephpinfo()Usually restart Apache
PHP-FPMphpinfo() or FPM diagnosticsUsually reload/restart PHP-FPM
Take Quiz

Cheat Sheet

:: Show the main php.ini and additional parsed INI files
php --ini

:: Show which PHP executable(s) Windows can find
where php

:: Show PHP version and extension startup warnings
php -v

:: List loaded modules
php -m

:: Check one extension specifically
php -m | findstr /I pdo_mysql

:: Display detailed runtime configuration
php -i

:: Check whether an extension is loaded
php -r "var_dump(extension_loaded('pdo_mysql'));"

Relevant php.ini directives:

extension_dir = "ext"
extension=php_pdo_mysql.dll

Key rules:

  • Trust Loaded Configuration File, not a guessed file path.
  • A leading ; disables an INI line.
  • The CLI and web server can use different PHP versions and different INI files.
  • Check php -v after edits to catch DLL loading warnings.
  • Confirm success with php -m or extension_loaded('pdo_mysql').

FAQ

How do I find the php.ini used by PHP CLI?

Run:

php --ini

Read the Loaded Configuration File line.

Why does php --ini say Loaded Configuration File: (none)?

The CLI did not load a main php.ini. Check the reported configuration path, create or copy an appropriate php.ini there if required by your installation, and run the command again.

How can I tell whether I have multiple PHP installations on Windows?

Run:

where php

If it shows multiple paths, verify which one is selected first and inspect it with php --ini.

Do I need to restart my computer after editing the CLI php.ini?

No. Each CLI PHP command starts a new PHP process and reads configuration again. Run php -m or your script after saving the file.

How do I confirm that pdo_mysql is enabled?

Run:

php -m | findstr /I pdo_mysql

Or run:

php -r "var_dump(extension_loaded('pdo_mysql'));"

Why does pdo_mysql work in my browser but not in the terminal?

Your web server and CLI are likely using different PHP executables, PHP versions, or php.ini files. Compare php --ini with the phpinfo() output from the web environment.

What does extension_dir do?

It tells PHP where to find extension files. On Windows, php_pdo_mysql.dll must exist in that directory and be compatible with the PHP executable.

Take Quiz

Mini Project

Description

Create a small PHP command-line diagnostic script that reports the active configuration file and checks whether the PDO and pdo_mysql extensions are available. This mirrors the first checks developers perform when a CLI database command cannot connect to MySQL.

Goal

Run one command that clearly reports whether the current PHP CLI is ready to use PDO with MySQL.

Requirements

Create a PHP file named check_mysql_driver.php. Display the PHP binary path and loaded php.ini path. Check both PDO and pdo_mysql with extension_loaded(). Exit with a non-zero status code if pdo_mysql is unavailable. Print a helpful next step when the driver is missing.

Take Quiz

Keep learning

Related questions

View more