7

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.

Sam Watkinson
  • 73
  • 1
  • 7
  • 2
    You should load the extension in your config file. – Lajos Veres Oct 10 '13 at 20:49
  • 3
    Check this out: http://stackoverflow.com/questions/666811/fatal-error-class-mysqli-not-found – immulatin Oct 10 '13 at 20:49
  • Lajos: Where can I find the config file? – Sam Watkinson Oct 10 '13 at 21:00
  • grep your hdd for php.ini, you may have more than one ... – Cups Oct 10 '13 at 21:01
  • 1
    immulatin: Thank you, I've been through that post and it has not solved my problem. mysqli IS installed and IS referred to in php.ini. – Sam Watkinson Oct 10 '13 at 21:01
  • 2
    probably didn't enable mysqli in your SAPI-based php installer (webserver-embedded), but it is enabled in your CLI-based version, both of which use DIFFERENT .ini files. – Marc B Oct 10 '13 at 21:23
  • Just run `phpinfo();` in the the code interpreted by the webserver, it will tell you exactly _which_ file to edit. – Wrikken Oct 10 '13 at 21:39
  • I am facing the exact same issue with Apache 2.4, PHP 5.5.12, Windows Server 2008. I tried almost everything now, but nothing worked. @Wrikken, where should I look for this in `phpinfo();` I couldnt find it. It mentions `Configuration File (php.ini) Path: C:\Windows`, However, I do not find such a file there. – Temp O'rary Apr 20 '20 at 19:29
  • Finally, I was able to fix this. Solution given below. – Temp O'rary Apr 20 '20 at 21:16

3 Answers3

1

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?

Joe McMahon
  • 3,266
  • 21
  • 33
jcubic
  • 61,973
  • 54
  • 229
  • 402
1

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.

Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
0

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/'.

Hossein
  • 1,301
  • 1
  • 12
  • 23
  • I am also facing the exact same issue with Apache 2.4, PHP 5.5.12, Windows Server 2008. Can you please give more info about the ini file for Apache? I am not able to locate it. – Temp O'rary Apr 20 '20 at 19:32
  • Finally, I was able to fix this. Solution given below. – Temp O'rary Apr 20 '20 at 21:16