How to enable mysqli support for PHP's CLI - php

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

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

Related

Call to undefined function mysqli_connect() in CLI but for non-root user only [duplicate]

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
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

Laravel - could not find driver when migrating

Hello I'm using latest version of Laravel, PHP 7.1.9 and MySQL 5.7.19. I'm also using WAMP stack.
When I run the command:
php artisan migrate
I get the following error:
I googled a lot and I' m 100% sure my php.ini file does not have the driver line commented out and the driver is properly working, as seen here:
The driver is obviously present:
Also the path to extension dir is absolute:
extension_dir ="c:/wamp64/bin/php/php7.1.9/ext/"
What else can I do? This thing is driving me crazy
There are 2 php.ini files in the WAMPServer implementation.
One for APACHE which is the one accessed from the menu system and one in each wamp\bin\php\phpx.y.z folder
As you are using the PHP CLI you have to check that all the required extensions are also activated in PHP you are using for the CLI wamp\bin\php\phpx.y.z
Do this from the command line
php --ini
It should show you something like this
Configuration File (php.ini) Path: C:\WINDOWS
Loaded Configuration File: C:\wamp64\bin\php\php7.0.29\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
Look at this param Loaded Configuration File: to see which php.ini file you should be checking.
This post may also be useful to you for making a batch file to run from the command prompt to switch easily from one version of PHP CLI to another from the command window How To Run PHP From Windows Command Line in WAMPServer

How do I get PHP command line to use a php.ini file when it is not using one?

This is for a Mac OS X. I need to put a line in the php.ini file for PHP 5.6 - however, I have found out from this:
php -r "php_ini_loaded_file();"
That PHP is not using an ini file!
OK, so that was a surprise... How do I either tell PHP to use an ini file, or add the line I would have added in that ini file?
Using php --ini gives me the same thing:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
The following worked for me. I typed
php --ini
which also told me that there was no config file, but it DID tell me the default directory was /etc.
So I did a sudo nano /etc/php.ini
and added a comment (starting with a ;) and did php --ini again - and it showed the file was present! Just to be sure, I added some illegal characters, and went back to php --ini which told me there was a parse error.
So it appears you can always add a php.ini file - even if it starts out blank.
You can find the command-line switches in the manual or with the help switch:
php -h
I believe you want one of these:
-c <path>|<file> Look for php.ini file in this directory
-d foo[=bar] Define INI entry foo with value 'bar'
Otherwise, PHP will search for the php.ini file according to the documented rules. I understand that CLI interface makes use of the PHPRC environment variable if it exists.

PHP module is shown in phpinfo(), but not php -m

I'm currently working on a website. I used the host's control panel to activate several php modules. Now when I try to use them it returns
Fatal error: Class '...' not found
I checked phpinfo() and the classes appear there, but when I do php -m, they are missing. The server uses Nginx.
What can be the problem and how can I fix it?
Your PHP web installation and your PHP cli installation can be two completely seperate things, and hence they usually have their own ini configuration files.
As per your comment, this is the output you get from your CLI phpinfo:
Loaded Configuration File: (none)
Scan for additional .ini files in: /usr/opt/php56/etc/php
Additional .ini files parsed: (none)
As you can see there's no "Loaded Configuration File". As per this post you can specify what ini file to load when running cli scripts.
Since your regular web installation says:
Loaded Configuration File /home/a2869511/etc/php.ini
Then you can automatically have your CLI program execute with the same ini file:
php --with-config-file-path=/home/a2869511/etc/php.ini script.php
That said, I would recommend that you take a copy of the ini file and place it where your cli application is looking for ini files, which is in /usr/opt/php56/etc/php ("Scan for additional .ini files in").
So the short solution would be to do the following copy:
cp /home/a2869511/etc/php.ini /usr/opt/php56/etc/php/php.ini

Changing the default PHP Command Path

I'm running Ubuntu Natty.
I recently installed PHP 5.3.8 from Source and it works ok. However, as part of my installation, I had to install php5-dev so I guess this has installed another PHP on the server (I needed to do this for phpize).
My main PHP binary is at /usr/local/php/bin. However, when I run php from the command line with something like this php --ini I get the following:
$ php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File: /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed: /etc/php5/cli/conf.d/pdo.ini
I can still run PHP from /usr/local/php/bin, but that would mean me having to type the whole path out for all my command line scripts.
Is there a way I can change this behaviour so that when I use the php command, it points to the php binary inside /usr/local/php/bin instead?!
Thanks in advance
You can edit your .bash_profile file to change your default search path. Or update it using PATH=/usr/local/php/bin:$PATH or somesuch.
The below code worked for the specific circumstance in my earlier question:
printf "\nexport PATH=/usr/local/php/bin:\${PATH}\n" >> /etc/profile
source /etc/profile
Hope it helps.

Categories