CodeIgniter中CLI模式无法调用oci_connect(),浏览器运行正常
Hey there, let's work through this issue together! The fact that your script runs fine in the browser but breaks via CLI tells us one key thing: your command-line PHP is using a different configuration than the one Apache (your web server) uses—and that CLI setup isn't loading the OCI8 extension. Here's how to fix it step by step:
1. Check your CLI PHP's extension status
First, let's confirm if the OCI8 extension is even loaded for CLI:
- Open a command prompt (CMD or PowerShell)
- Run these commands:
php -v # This shows which PHP version CLI is using php -m # Lists all loaded extensions—look for "oci8" here
If you don't see oci8 in the list, that's exactly the problem.
2. Locate the CLI-specific php.ini file
WAMP keeps separate php.ini files for web and CLI. To find the one your command line uses:
- Run:
php --ini
Look for the line that says Loaded Configuration File—this is the php.ini you need to edit (it's probably in C:\wamp64\bin\php\php[your-version]\php.ini, not the one in the Apache folder).
3. Enable the OCI8 extension in CLI's php.ini
Open that php.ini file in a text editor and make these changes:
- Find the line starting with
;extension=oci8(or;extension=oci8_12c, depending on your Oracle version) and remove the leading semicolon to uncomment it. It should look like:extension=oci8 - Double-check the
extension_dirsetting points to the correct extensions folder. For most WAMP setups, this should be:
Or use the full path, e.g.,extension_dir = "ext"extension_dir = "C:\wamp64\bin\php\php[your-version]\ext"
4. Ensure Oracle client libraries are accessible to CLI
The CLI environment needs to find Oracle's client DLLs (like oci.dll). Here's how to fix that:
- Add your Oracle client's
bindirectory to your system's PATH environment variable (e.g.,C:\instantclient_19_12). - Make sure the Oracle client's architecture matches your PHP version (32-bit PHP needs 32-bit client, 64-bit PHP needs 64-bit client—mixing these will cause failures).
- After updating PATH, close all open command prompts and reopen them to apply the change.
5. Verify and test
- Reopen your command prompt and run
php -magain—you should now seeoci8in the list of loaded extensions. - Navigate to your CodeIgniter project root and run your script via CLI, e.g.:
php index.php YourController YourMethod
This should now work without the oci_connect() error.
Common Gotchas to Watch For
- Don't confuse the web php.ini (in Apache's bin folder) with the CLI php.ini—they're separate, so editing the web one won't affect CLI.
- If you're still having issues, double-check that your OCI8 extension version is compatible with your PHP version (e.g., PHP 8.0 needs a compatible OCI8 build for 8.0).
内容的提问来源于stack exchange,提问作者Hana90

