PHP mysqli works in command line, but not on pages

2019-06-27 04:32发布

问题:

Red Hat Linux Server, Apache 2.2, PHP 5.5.4

When I run this in the command line, everything seems to be fine:

php -r '$mysqli = new mysqli("127.0.0.1", "un", "pw", "things", "3306");'

At the same time, I have a .php document which doesn't run. I get this error...

PHP Fatal error: Class 'mysqli' not found in [/path/to]/vars.php on line 2

from this code:

<?php
    $mysqli = new mysqli("localhost", "un", "pw", "things", "3306");
?>

Other .php pages (which don't use mysqli) work fine. Any idea what's going on here? Thanks in advance.

回答1:

running php from CLI(command line) is deffent from webserver.

Ushould enable mysqli extension in both.

Normally, if it is installed, you can go into 'php.ini' and uncomment the line for 'mysqli'. On some installations, there is one copy of 'php.ini' for Apache and another for the command line. I think they are both located somewhere under '/etc/php5/'.



回答2:

I've ask almost the same question on Unix stack exchange and the issue get resolved. Sometimes restarting apache take no effect even rebooting the system, but stoping the service and starting it again worked for me:

On Fedora

sudo systemctl stop httpd && sudo systemctl start httpd

on system without systemd (like Ubuntu) you can probably can use something like this, didn't test it:

sudo service apache2 stop && sudo service apache2 start

reference: How to install php + mysql on Fedora?