Fatal error: Class ‘MySQLi’ not found on line

A common error that occurs, particularly for newcomers, to SQL development is a fatal error when trying to refer to the MySQLi class such as:

Fatal error: Class 'MySQLi' not found ([URL]) on line [X]

The Issue

The issue being caused here is that the class, in this case ‘MySQLi’ cannot be initialised. Forget for a moment that this is a MySQLi issue and consider that it is actually an issue that you can encounter with any object-orientated programming development. The fundamental issue here is that you’re trying to load a class that isn’t or cannot be found.

Initialising the MySQLi Class

You will likely be using a simple bit of code to initialise your MySQLi class. Something like:

$mysqli = new MySQLi($db_server, $db_user, $db_password, $db_name);
  • $mysqli – this is the object where you want the reference to the new MySQLi instance to be stored.
  • $db_server – the address to your database server.
  • $db_user – the username for use with your database (server-wide or database-specific).
  • $db_password – the password for the database user stored in $db_user
  • $db_name – the database table that you want to access.

It’s always worth double-checking both the structure of your initialisation code to ensure there are no typos or missing parameters as this is a common cause. Also double-check all the details you are providing in your parameters.

Check your MySQLi installation

A common reason for the above error occurring is simply forgetting to install MySQLi. This can easily be done locally during development by going to the PHP website for MySQLi installation. On Windows machines you will likely find that you don’t (or haven’t) had to do this manually if you’ve used the binary installer. The MySQLi extension is enabled by default for PHP versions 5.3.0 onwards.

If you have used the binary installer and it’s still not working, trying to reinstall PHP to see if it resolves the issue as there could have been a failed installation or configuration that didn’t complete correctly.

If you are running your code on AWS, you can use a command such as:

sudo yum install php71-mysqli

This will trigger the installation of MySQLi.

To install the native driver on Linux you would need something like:

sudo apt-get install php-mysqlnd

Things to double check when initialising the MySQLi Class

If you’re still having trouble instantiating the MySQLi class after checking the installation you can try the following common resolutions to issues:

  • You have included a well structure call to the MySQLi constructor
  • The values for your parameters/variables have been initialised prior to the call
  • The values passed to the MySQLi constructor are correct.
  • The database user specified exists and has privileges for the table you have specified – either global access or a database specific user.
  • The database table you are passing in exists.

Hope that helps! If you have any more tips or issues, drop them in the comments below!

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x