命令行 php mysqli 失败但通过 Web 服务器工作并在本地工作

2024-04-13php开发问题
1

本文介绍了命令行 php mysqli 失败但通过 Web 服务器工作并在本地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 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 服务器(CentOS6 安装了 'yum install php' 和 PHP 5.3.x).我更改了与远程 db 服务器的连接,因此删除了/tmp/mysql.sock",因为 db 未在此服务器上运行.

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 lib 并执行.它在 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 服务器工作并在本地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17