Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
917 views
in Technique[技术] by (71.8m points)

linux - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...