Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysqli works in command line, but not on pages

Tags:

php

apache

mysqli

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.

like image 948
Sam Watkinson Avatar asked Oct 10 '13 20:10

Sam Watkinson


People also ask

How can I tell if mysqli is enabled in PHP?

Check if MySQLi is Installed You can do that by visiting a phpinfo() page that you made, or by running this command: php -m | grep mysqli.

How configure mysqli in PHP?

It'll automatically enable the mysqli extension for the PHP because connect using mysql is deprecated in PHP 7. If not an Ubuntu user then you can just rename php-prodcution. ini file to php. ini and enable the extension by removing semicolon in the php.

Is mysqli deprecated in PHP 7?

The oldest one uses the MySQL extension, which was deprecated as of PHP 5.5 and fully removed in PHP 7. The mysql() function no longer works in PHP 7. It has been replaced with mysqli().


2 Answers

Like I said, today I was facing the exact same issue with Apache 2.4, PHP 5.5.12, Windows Server 2008.
But I was able to solve this.

Diagnostics:
I ran phpinfo() in both my command prompt as well as on the web server/browser and noticed that path of the php.ini files were different in both. Now, I realised that I need to force set the path to the php.ini file in Apache.

Solution:
I fixed this by pointing to the correct php.ini and dll files on my Apache server. In the httpd.conf file I wrote these lines in sequence.

PHPIniDir "C:/path/php/php5.5.12"
LoadFile "C:/path/php/php5.5.12/php5ts.dll"
LoadModule php5_module "C:/path/php/php5.5.12/php5apache2_4.dll"  

The first line alone fixed the issue.

like image 83
Temp O'rary Avatar answered Oct 01 '22 20:10

Temp O'rary


I've asked almost the same question on Unix Stack Exchange; this was how the issue got resolved.

Sometimes restarting Apache has no effect even after rebooting, but stopping the httpd service and starting it again worked for me:

On Fedora

sudo systemctl stop httpd && sudo systemctl start httpd

On systems 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?

like image 37
jcubic Avatar answered Oct 01 '22 21:10

jcubic