Fatal error: Call to undefined function mysqli_connect()

So you’ve come across this error, you’ve changed every line of code 3000 times and still it won’t work. What gives?

This error is stubbornly staring back at you:

Fatal error: Call to undefined function mysqli_connect()

The reason why no matter how much you change and tweak nothing seems to work is that your code isn’t wrong. Or, more specifically, your code may be wrong – but it isn’t causing this error!

The cause

Essentially this error is being thrown by PHP as it doesn’t have the first clue what mysqli_connect() is. In other words, it doesn’t exist.

BUT I hear you call out – everybody is using it! They are – and the reason they can use it but your current setup isn’t allowing it is because you don’t have the appropriate MySQLi adapters installed along correctly on your server.

How to check

A simple way to check this is running the phpinfo() function. Comment everything out (or create a new test file) and place the following line of code:

phpinfo();

Running this page should now present you with a raft of information about your PHP setup – this will work on both a web server and a local development server.

You’ll get a page of output looking similar to the below:

It’s easy to get overwhelmed with all the data, so we’re looking for something very specific here. Scroll down or search (CTRL+F or CMD+F on Mac) for ‘mysqli’. It should be on it’s own section and, if the plugin is installed correctly you should notice that it says below the title ‘Mysqli Support – enabled’:

You could also us this piece of code to confirm what you already know if you don’t want to scroll through the phpinfo() list:

console.log(function_exists('mysqli_connect'));

This will return TRUE if the function exists – and therefore the plugin is active and working – or false if it doesn’t. However, given the error message above says that mysqli_connect() is undefined, you can bet a fairly safe bet you will get false returned here.

How to install MySQLi support for PHP

On a Linux based server, in the terminal you can enter the following command to retrieve and install the php-mysql package to enable support for the mysqli commands:

sudo apt-get install php-mysql

You may need to reboot your server after installation is completed.

Using EasyApache (for example, through Web Host Manager – WHM) makes it even easier and you can avoid the terminal/command line if you’re not a fan of it!

  1. Login to WHM as the main (usually ‘root’) user.
  2. Select ‘EasyApache’ from the menu on the right hand side.
  3. Select your ‘Previously Saved Config’ – this will ensure you keep all your existing profile settings from the last time you complied/recompiled Apache.
  4. Click the ‘Start’ button to start customising the profile.
  5. You can keep your Apache and PHP versions the same by clicking the ‘Next’ button.
  6. For the additional options, you’ll need to select the MySQL Improved extension from the ‘Exhaustive Options List’.
  7. Save and build. Once finished Apache should automatically restart and if you re-run your code above it should now work as MySQLi support has been added.

Still not working?

The final step, if it isn’t automatically done (which it usually is) is to ensure that the php.ini file (your PHP configuration settings file) is updated to use the new MySQLi plugin.

Open the PHP.ini file and search for:

extension=php_mysqli.so

This line may be commended out (using a semicolon – 😉 at the beginning of the line. If you see a semi-colon, remove it, save your PHP.ini file.

Finally, restart your Apache server (either through your control panel or, if running locally, quit the server and restart the application) and hey presto, your code should run like a dream now!

Hope that helps – let us know any 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