Mysql – CentOS 7 thesqli works on php apache but not on php-cli

centos7MySQLphp-cliphp7

I have php7.2 installed(from remi repository) in CentOS 7.5. I want to access mysqli from command line php. When trying, it shows the error:

Call to undefined function mysqli_connect()

The php running with apache works fine with mysqli. All extensions installed through yum reflects perfectly here. But for the php command line(which is parsing a different php.ini file), the extensions are not loaded.

Do I need to separately add the extensions for commandline php? If so how? Or any other alternative? Please let e know if you need any further information.

Thank you for your time.

Best Answer

As pointed out by Michael Hampton (thanks) in the comment, there were two php installations in the server. I have arrived at two different solutions based on specific need:

  1. For php-cli:

In the .bash_profile, the following line is added

alias php="/usr/bin/php"
  1. For running PHP from java servlet (as command):

Use

String[] commands = {"bash", "-c", "/usr/bin/php myphpfile.php arg01 arg02"};
File dir = new File("<my-php-file-directory>");
try{
Process process;
process = Runtime.getRuntime().exec(commands, null, dir);
process.waitFor();
....
....
}

Thats all

php -modules in the terminal gives the desired output with mysqli.