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
1.1k views
in Technique[技术] by (71.8m points)

shell - Command line php mysqli fails but works via web server and works in local

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.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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.


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

...