Command line php mysqli fails but works via web se

2020-04-11 06:21发布

问题:

I have a shell script that calls a php script to insert records in database after parsing text file. When it calls the PHP script I get errors that it cannot find mysqli:

PHP Fatal error:  Class 'mysqli' not found in /path/to/php/file.php on line 5

This failed in local on Macbook Pro with Nginx and PHP-FPM and Mysql 5.5.x and after I added the socket param to mysqli call, it worked in local. This worked with local db!

$mysqli = new mysqli("localhost", "user", "pass", "mydb", 3306, "/tmp/mysql.sock");

I deployed to Linux server (CentOS6 that has 'yum install php' with PHP 5.3.x) installed. I changed the connection to a remote db server so dropped the "/tmp/mysql.sock" because db not running on this server.

Shell script calling php to execute script:

php -c "/etc/php.ini" -f "/path/to/php/file.php"

Given I am not running mysql locally, how do I get it to recognize the mysqli lib and execute. It's on CentOS6. I even tried adding mysqli.so to php.ini file and got following error:

[mike@app2 myapp]# php -version
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/mysqli.so' - /usr/lib64/php/modules/mysqli.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP 5.3.3 (cli) (built: Feb  2 2012 23:47:49) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

Thanks for any help resolving this. Normally I'd use mysql(query) but need mysqli() for some prepared statements and other bits in the script.

回答1:

I found that mysqli was not installed with PHP and ran the following command:

>php -i

This returned the phpinfo() and found no mention of mysqli. I then ran the 'yum' command to install php-mysqli:

>yum install php-mysqli

After this the script worked successfully. Hope this helps others that might run into similar.



回答2:

PHP CLI will have its own environmental setup from that used by the webserver. If you don't have mysqli available in the CLI environment that would cause this issue.