The error you are getting is a follow-on error, having nothing directly to do with the actual problem. It’s because the posted connection code has failed, but there’s no error handling to stop program execution. The code continues to run past the point where the problem is at and finally produces a fatal runtime error when something is called that’s dependent on a variable holding a valid connection. You also apparently don’t have php’s error_reporting set to E_ALL. The posted connection code would be producing a php warning message.
You ALWAYS need error handling for statements that can fail. For database statements, the easiest way of adding error handling is to simply use exceptions for errors and in most cases let php catch and handle the exception, where it will use its error related settings to control what happens with the actual error information.
To enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the database connection -
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Doing this will at least stop code execution at the point where the first database statement is failing.
So, what is wrong with the posted connection code -
$database = new Mysqli();
$db = $database->connect();
This is redundant and is misusing the value from the ->connect() method call. You would normally just use the first line of code, either supplying the connection credentials as call-time parameters or setting them as the default connection values in a php.ini/htaccess file. If you did have a case where you used the first line to just create an instance of the mysqli class, without making a connection, then call the ->connect() method, the ->connect() method doesn’t return the connection. It just makes the connection, which would be available via the $database variable. Despite the documentation for the ->connect() method, it returns a false if the connection fails, and returns void/null if the connection works. This is where the boolean false value is coming from in the prepare() method call. $db is not the connection. $database would be the connection.
Short-answer: 1) ALWAYS have error handling for statements that can fail, 2) make sure php’s error_reporting is ALWAYS set to E_ALL, and 3) use the simplest code to accomplish a task. You only need one line of code to make a database connection.