PHP changing to mysqli. Is the mysqli_connection not global?
Yes, MySQLi is object oriented and you need to pass around the $db
object to the functions that need a database connection. The procedural alternatives also require the $db
object to work. This is for your own good, as implicit global connections (actually: global state in any form) are bad practice. That the mysql extension allowed implicit connections was not good to begin with.
Here's an option - create a static class purely to hold the mysqli object as a public static variable:
class DBi { public static $conn;}DBi::$conn = new mysqli(HOST, USER, PASS, DB);
... in an include file, database helper or whatever.
Then wherever you want to make a mysqli call you do
DBi::$conn->query(...)
Makes it as global as you could want. And you can easily start popping your own custom functions (methods) in there if you feel the need - DBi::custom_method()
This is kind of a refinement to the answer given at Converting mysql to mysqli - how to get superglobal connection object?