Jump to content

Fatal error: Uncaught Error: Class "mysqli" not found...


Jagboy
Go to solution Solved by kicken,

Recommended Posts

I'm trying to get PHP8 to play nice with SQL, but only get the above error, when trying to create the mysqli object.  All the code has been working fine for years on a Rapsberry Pi, but I've recently had to move to a NanoPi-R5C.  PHP and SQL both work fine.  phpinfo() says mysqli is installed, and it shows the correct library path and filename.  I've also tried mysqlnd with the same result.  I cannot figure out why it does not work.

Any ideas?  I've been stuck on this all day...

Link to comment
Share on other sites

I have never seen a situation where an extension showed up in phpinfo and yet didn't work. It would also be helpful to know what OS you are running on the device.

Is this a cli program or through a web server? 

If you run a simple script like this, what does it indicate?

 

<?php
if (function_exists('mysqli_connect')) {
  echo "mysqli is installed";
} else {
  echo "Enable Mysqli support in your PHP installation "; 
}

 

Link to comment
Share on other sites

One other detail:  I'm not sure what you mean by "tried mysqlnd".  You should use mysqlnd, but that is the client library that mysqli depends upon, and should appear in the mysqli section of phpinfo.  The other option is to use the libmysqlclient from oracle.  The default since php5.4 has been mysqlnd.

Link to comment
Share on other sites

18 hours ago, requinix said:

Sounds like you don't have the mysqli extension installed or enabled...

phpinfo() says mysqlnd in installed, and when I install msqli I get a similar display.  AFAICT, there is nothing to do to "enable" it.  If it's installed, it's enabled.  Is there something I don't know (HIGHLY likely!!)?

image.thumb.png.e0ef159c917048ac3454d18af473bafd.png

 

Link to comment
Share on other sites

9 hours ago, gizmola said:

I have never seen a situation where an extension showed up in phpinfo and yet didn't work. It would also be helpful to know what OS you are running on the device.

Is this a cli program or through a web server? 

If you run a simple script like this, what does it indicate?

 

<?php
if (function_exists('mysqli_connect')) {
  echo "mysqli is installed";
} else {
  echo "Enable Mysqli support in your PHP installation "; 
}

 

When I run your code snippet, it says "Enable Mysqli support in your PHP installation".  HOW do I "enable Mysqli"??

Edited by Jagboy
Link to comment
Share on other sites

9 hours ago, gizmola said:

One other detail:  I'm not sure what you mean by "tried mysqlnd".  You should use mysqlnd, but that is the client library that mysqli depends upon, and should appear in the mysqli section of phpinfo.  The other option is to use the libmysqlclient from oracle.  The default since php5.4 has been mysqlnd.

This is far from my area of expertise, so I may be completely misunderstanding things.  My understanding is that mysqlnd is, effectively, a replacement for mysqli, which is basically API-compatible, so the same PHP script should run with either.  Is my understandingn wrong?  On the RPi-3B, which I was using until recently, I used mysqli and had no problems.  Anyway, whether I install mysqli alone, without mysqlnd, or install my mysqlnd alone, without mysqli, when I run my PHP script using php-cli, I get: "Fatal error: Uncaught Error: Class "mysqli" not found...".

I would be thrilled to find out I am just an idiot, and doing something stupid.  That would be the best solution!

Link to comment
Share on other sites

I don't know what RPi-3B is and I'm not a SysAdmin but I will say the if you're starting a new project with no skin in the game, go with PDO over MySQLi. It's much easier to work with and (I think) enabled by default. (I think.) Either way, I know it's easier to deal with while coding. mysqli is not a direct corollary to the removed mysql_* functions so no matter what you're going to have to refactor a good amount of code.

Link to comment
Share on other sites

10 minutes ago, Jagboy said:

when I run my PHP script using php-cli

The CLI version of PHP can have a different configuration than the version you'd get with whatever http server you're using.  You can check the modules loaded in the CLI version by running php -m or get full phpinfo like output using php -i.  Use either of those to test if mysqli is enabled. 

php -m | grep mysqli

If it's not enabled, you can find relevant configuration files with

php -i | grep .ini

Look at the INI files to determine how to enable mysqli for CLI.

 

Link to comment
Share on other sites

38 minutes ago, maxxd said:

I don't know what RPi-3B is and I'm not a SysAdmin but I will say the if you're starting a new project with no skin in the game, go with PDO over MySQLi. It's much easier to work with and (I think) enabled by default. (I think.) Either way, I know it's easier to deal with while coding. mysqli is not a direct corollary to the removed mysql_* functions so no matter what you're going to have to refactor a good amount of code.

RPi-3B is the RaspberryPi v3B board.  On that version of this system, the OS is Debian Linux, while the new system (a NanoPi R5C) is running OpenWRT (another Linux variant).

As I said, I truly hope I am simply doing someting stupid here.  On the RPI3, I installed only the apache2 package, and the php-mbstring software package, and it just worked, as if by magic.

I am not familiar wiith PDO.  How different is it?  I can re-write for PDO if I absolutely must.

I did try running the script through the web server, rather than php-cli, but got the same result.

Link to comment
Share on other sites

1 hour ago, kicken said:

The CLI version of PHP can have a different configuration than the version you'd get with whatever http server you're using.  You can check the modules loaded in the CLI version by running php -m or get full phpinfo like output using php -i.  Use either of those to test if mysqli is enabled. 

php -m | grep mysqli

If it's not enabled, you can find relevant configuration files with

php -i | grep .ini

Look at the INI files to determine how to enable mysqli for CLI.

 

php-cli -m | grep mysqli prints "mysqli".  php-cgi does the same.

I see nothing obvious in php.ini to enable php.  The error message shows it does have the correct path for the mysqli library file.  Numerous searches have reinforced my impresstion that simply installing the module should enable it, without changing anything in the ini file.

 

 

Link to comment
Share on other sites

1 hour ago, requinix said:

Then you do have it installed. What code isn't working and what is the error message? Please make sure to copy and paste exactly what you have/see.

 

See below files and screen shotimage.thumb.png.dc0d1d59b9828c8f1623442169b0895c.png

 

.  CommonDefs gets included into NewEvent.  The first executable line in NewEvent is a call to connect in CommonDefs, which tries to create the mysqli object, which fails.  Though now it is giving a different message (I don't know why?), the error is still at the same point in the code - the call to the mysqli constructor.

CommonDefs.php.txt NewEvent.php.txt

Link to comment
Share on other sites

That error is not Fatal error: Uncaught Error: Class "mysqli" not found, it's

Quote

Fatal error: Uncaught mysqli_sql_exception: No such file or directory

Your mysqli extension exists and is working just fine, the error is because your connection is failing with a no such file or directory system error.

My guess is this is due to mysql trying to use a unix socket to connect, but that socket does not exist.  Mysql will try and use a unix socket instead of TCP when you specify 'localhost' as the hostname.

Quote

A Unix socket file is used if you do not specify a host name or if you specify the special host name localhost

Try changing the hostname in your connect call to 127.0.0.1 instead.

Edited by kicken
Link to comment
Share on other sites

This looks like progress!  Changing the host to 127.0.0.1 I get:

 

Fatal error: Uncaught mysqli_sql_exception: Can't read dir of './qdtimerdata/' (errno: 13 "Permission denied") in /root/NewEvent.php:33
Stack trace:
#0 /root/NewEvent.php(33): mysqli->query('SHOW TABLES;')
#1 {main}
  thrown in /root/NewEvent.php on line 33

I assume this is a permissions problem in SQL?

Link to comment
Share on other sites

  • Solution
3 hours ago, Jagboy said:

I assume this is a permissions problem in SQL?

Sounds like a permission problem with the directory where mysql stores it's database files.  Seems like your mysql setup must have not be done properly (no unix socket, bad data dir permissions, who knows what else).

Link to comment
Share on other sites

1 hour ago, kicken said:

Sounds like a permission problem with the directory where mysql stores it's database files.  Seems like your mysql setup must have not be done properly (no unix socket, bad data dir permissions, who knows what else).

Thank you so much!  I'm sure I can sort out the sql problem.  I never would have figured out the localhost thing, without you pointing me in the right direction.

What really surprises me is I had none of these problems when I set this all up on the Rapsberry Pi.  I just installed the software, did some very simple configuration, and it all just worked perfectly.

Link to comment
Share on other sites

Linux distros for the most part have their own package managers, and package developers who sometimes have schemes they are working with that differ from what another distro might use.  Anytime you set up things on a different distribution you will likely encounter these differences and perhaps a problem or 2.  

I'm not sure why localhost wouldn't work, if you have a valid /etc/hosts file with an entry for it, but mysql also has a lot of potential configuration possibilities that change the behavior.  By default it will use a unix socket, rather than trying to route through the network layer.  Telling it to use 127.0.0.1 forces the client to try and use the network layer.

This serverfault thread explores the issue and potential fixes.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.