How to enable mysqli support for PHP's CLI

做~自己de王妃 提交于 2021-02-02 09:25:46

问题


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.


回答1:


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



回答2:


test.php:

if (!extension_loaded('mysqli'))  {
        dl('mysqli.so');
}

in cli,run it:

php test.php

os output:

Warning: dl(): Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20131226/mysqli' - /usr/local/lib/php/extensions/no-debug-non-zts-20131226/mysqli: cannot open shared object file: No such file or directory in test.php

you can find and copy mysqli.so from any where, for example: /usr/local/lib/php/extensions/no-debug-non-zts-20131226$ sudo cp /usr/lib/php5/20131226/mysqli.so ./mysqli.so



来源:https://stackoverflow.com/questions/28510927/how-to-enable-mysqli-support-for-phps-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!