3

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.

Arya
  • 566
  • 2
  • 9
  • 22
  • 2
    Please try `php --ini` just to make absolutely sure it's using the ini you think it is – rjdown Feb 14 '15 at 00:24
  • @rjdown: I tried `php --ini` and it showed: `Configuration File (php.ini) Path: /usr/local/lib`. `Loaded Configuration File: (none)`. `Scan for additional .ini files in: (none)`. `Additional .ini files parsed: (none)` – Arya Feb 14 '15 at 00:38
  • [http://stackoverflow.com/questions/21972038/php-mysqli-not-working][1] sudo apt-get install php5-mysql This solves the problem. [1]: http://stackoverflow.com/questions/21972038/php-mysqli-not-working – Le chat du rabbin Sep 18 '15 at 11:32

2 Answers2

1

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

Xuehui Xie
  • 11
  • 1
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
symcbean
  • 47,736
  • 6
  • 59
  • 94