問題描述
我有一個 shell 腳本,它調用一個 php 腳本在解析文本文件后在數據庫中插入記錄.當它調用 PHP 腳本時,我收到找不到 mysqli 的錯誤:
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
這在使用 Nginx 和 PHP-FPM 以及 Mysql 5.5.x 的 Macbook Pro 本地失敗,在我將套接字參數添加到 mysqli 調用后,它在本地工作.這適用于本地數據庫!
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");
我部署到 Linux 服務器(安裝了yum install php"和 PHP 5.3.x 的 CentOS6).我更改了與遠程數據庫服務器的連接,因此刪除了/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腳本調用php執行腳本:
Shell script calling php to execute script:
php -c "/etc/php.ini" -f "/path/to/php/file.php"
鑒于我沒有在本地運行 mysql,如何讓它識別 mysqli 庫并執行.它在 CentOS6 上.我什至嘗試將 mysqli.so 添加到 php.ini 文件并得到以下錯誤:
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
感謝您幫助解決此問題.通常我會使用 mysql(query) 但需要 mysqli() 來處理一些準備好的語句和腳本中的其他位.
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.
推薦答案
發現mysqli沒有安裝php,運行如下命令:
I found that mysqli was not installed with PHP and ran the following command:
>php -i
這返回了 phpinfo() 并沒有發現 mysqli.然后我運行'yum'命令來安裝php-mysqli:
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.
這篇關于命令行 php mysqli 失敗,但可通過 Web 服務器運行并在本地運行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!