如何为 PHP 的 CLI 启用 mysqli 支持 [英] How to enable mysqli support for PHP's CLI

查看:29
本文介绍了如何为 PHP 的 CLI 启用 mysqli 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Ubuntu LTS 14.04 操作系统,我正在尝试在 PHP CLI 中测试我的 PHP 脚本,但是无论我的代码尝试连接到 MySQL 的任何地方,都使用诸如...

$mysqli = mysqli_connect($host,$user,$password,$database);

...,我收到以下错误:

致命错误:调用未定义的函数 mysqli_connect()...

我已经查看了 /etc/php5/cli/php.ini/etc/php5/apache2/php.ini 并没有发现任何区别.>

我认为我必须为命令行界面 (CLI) 启用 mysqli 支持,但我不确定.

如何在不影响当前 Apache php.ini/configuration/installation 的情况下更正错误?

编辑:
根据评论,我在终端中运行了以下命令:

php --ini

显示:

配置文件(php.ini)路径:/usr/local/lib加载的配置文件:(无)在以下位置扫描其他 .ini 文件:(无)解析的其他 .ini 文件:(无)

然后,我将/etc/php5/cli/php.ini复制到/usr/local/lib/php.ini.

然后,我再次运行php --ini,显示:

配置文件(php.ini)路径:/usr/local/lib加载的配置文件:/usr/local/lib/php.ini在以下位置扫描其他 .ini 文件:(无)解析的其他 .ini 文件:(无)

然后,我再次从 PHP CLI 运行 PHP 脚本,并显示相同的错误.

解决方案

调用未定义的函数mysqli_connect()

表示未加载mysqli扩展.

<块引用>

我已经查看了/etc/php5/cli/php.ini 和/etc/php5/apache2/php.ini 并没有发现任何区别.

如果它们相同,我会感到非常惊讶.但在这两种情况下,这些很可能被构建为与通过包分发的扩展一起使用 - 即包含加载 mysqli 指令的 ini 文件,因此扩展可能驻留在其他地方,并从这些文件中包含(或不包含).

<块引用>

配置文件(php.ini)路径:/usr/local/lib

你有一个弗兰肯斯坦系统.你需要清理它.使用发行版的打包应用程序或从 tarball 安装应用程序.除非您知道自己在做什么,否则不要使用 tarball.除非您真的知道自己在做什么,否则不要混合搭配.

  • 删除 tarball PHP 文件
  • 重新安装 Ubuntu PHP cli
  • 重新安装 Ubuntu PHP mysqli 扩展

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.

解决方案

Call to undefined function mysqli_connect()

Means the mysqli exension is not loaded.

I've reviewed /etc/php5/cli/php.ini AND /etc/php5/apache2/php.ini and have found no difference.

I would be very surprised if they were same. But in both cases, these are likely to be structured to work with extesnions distributed via packages - i.e. the ini file containing the directive to load the mysqli so extension will likely reside elsewhere and be included (or not) from these files.

Configuration File (php.ini) Path: /usr/local/lib

You have a Frankenstein system. You need to clean this up. Either use the distro's packaged apps or install apps from tarball. Don't use tarballs unless you know what you're doing. Don't mix and match unless you really know what you are doing.

  • Delete the tarball PHP files
  • reinstall the Ubuntu PHP cli
  • reinstall the Ubuntu PHP mysqli extension

这篇关于如何为 PHP 的 CLI 启用 mysqli 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆