Fatal Error: Call to a member function set_charset

It’s been a few years since I’ve been in my PHP code and the MAMP app. I tried going to test.myExampleDomain.com, but I get the following error:

Fatal error : Uncaught Error: Call to a member function set_charset() on boolean in /Users/robert/Sites/ot-website/database/connect.php:44 Stack trace: #0 /Users/robert/Sites/ot-website/public_html/siteDatabase.php(4): require_once() #1 /Users/robert/Sites/ot-website/public_html/index.php(4): include(‘/Users/robert/S…’) #2 {main} thrown in /Users/robert/Sites//ot-website/database/connect.php on line 44

Here is line 44 from connect.php:


In the MAMP app, I have PHP set to use version 7.2.1

Can someone help me figure out how to fix this so I can get my test environment back up?

Thank you!

The error means that $mysqli is (probably) a false value, because the connection failed, but you have no error handling to let you know that the connection failed or to stop the code from executing and trying to use the result from a statement that has failed.

You always need error handling for statements that can fail. For database statements that can fail - connection, query, prepare, execute, and a few others, the simplest way of adding error handling, without adding logic at each statement, is to use exceptions for errors and in most cases simply let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)

To enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -


Thank you for the reply, but I do have some basic error checking already:

$mysqli = mysqli_connect( '', $databaseUser, $databasePassword, $databaseName);

if ($mysqli->error) {
	exit ('<p>The website is experiencing extremely high levels of traffic right now.  Please try again later.</p>');

} else {
	//echo '<p>Established a connection with the requested user.</p>';


Knowing this, what would you change or add to your response?

Your error checking is invalid. The procedural mysqli_connect() call returns a false value upon a failed connection. There is no $mysqli->error property to test in this case, which will result in a false value, indicating no error, but for which there was actually a connection error. Also, the reference to the $mysqli->error property produces a lower php error level than the one that you are current getting for calling a method, which indicates that your php error_reporting is not set to E_ALL.

So, a bunch of things -

  1. Don’t mix mysqli procedural and OOP notation.
  2. Set php’s error_reporting, in your php.ini, to E_ALL, so that php will report all the errors it detects.
  3. Use exceptions for database statement errors, which will allow you to simplify your code.
  4. If you still want to fill up your code with unnecessary error handling logic, and use mysqli’s procedural mysqli_connect(), read the php.net documentation for how to test for connection errors.
  5. If you are just starting or re-starting using a database with php, switch to the much simpler and more modern PDO database extension.

How come this code works just fine in production? It’s only my test environment that produces this error. The whole site is being redone with React and Django, so I just need a quick fix for now.

Thanks @mabismad!

Because in production your mysqli_connect call doesnt fail.

In your test environment, you’ve messed something up - one of the four variables is incorrect or invalid. (or the server’s not running, or… )

The link for mab’s point #4 is https://www.php.net/manual/en/mysqli.construct.php ; it shows you there how you can get error messaging from the connect call. mysqli->error is for error handling in statements, not the connection.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.