简体   繁体   中英

How to enable mysqli support for PHP's CLI

I'm using Ubuntu LTS 14.04 operating system and I'm trying to test my PHP scripts in the PHP CLI, but wherever my code attempts to connect to MySQL, with commands such as...

$mysqli = mysqli_connect($host,$user,$password,$database);  

...,I get the following error:

Fatal error: Call to undefined function mysqli_connect()...  

I've reviewed /etc/php5/cli/php.ini AND /etc/php5/apache2/php.ini and have found no difference.

I think that I must enable mysqli support for the command line interface (CLI), but I am uncertain.

How can I correct the error without affecting my current Apache php.ini/configuration/installation?

EDIT :
Based on comments, I ran the following command in terminal:

php --ini  

Which displays:

Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Then, I copied /etc/php5/cli/php.ini to /usr/local/lib/php.ini .

Then, I ran php --ini again, which displays:

Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File:         /usr/local/lib/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Then, I ran the PHP script from PHP CLI again, and the same error displayed.

test.php:

if (!extension_loaded('mysqli'))  {
        dl('mysqli.so');
}

in cli,run it:

php test.php

os output:

Warning: dl(): Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/mysqli' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/mysqli: cannot open shared object file: No such file or directory in test.php

you can find and copy mysqli.so from any where, for example: /usr/local/lib/php/extensions/no-debug-non-zts-20131226$ sudo cp /usr/lib/php5/20131226/mysqli.so ./mysqli.so

Call to undefined function mysqli_connect()

Means the mysqli exension is not loaded.

I've reviewed /etc/php5/cli/php.ini AND /etc/php5/apache2/php.ini and have found no difference.

I would be very surprised if they were same. But in both cases, these are likely to be structured to work with extesnions distributed via packages - ie the ini file containing the directive to load the mysqli so extension will likely reside elsewhere and be included (or not) from these files.

Configuration File (php.ini) Path: /usr/local/lib

You have a Frankenstein system. You need to clean this up. Either use the distro's packaged apps or install apps from tarball. Don't use tarballs unless you know what you're doing. Don't mix and match unless you really know what you are doing.

  • Delete the tarball PHP files
  • reinstall the Ubuntu PHP cli
  • reinstall the Ubuntu PHP mysqli extension

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM