I have a separate file with an instantiated mysqli object which is in the a folder called includes within the root called connect.php:
$db = new mysqli($host, $user, $password, $dbname);
if ($db -> connect_errno) {
echo $db -> connect_error;
}
My first page has a require and uses the object $db to carry out a real_escape_string task which works well. I have a functions.php in the includes folder also. Within that there is a function called sendToDB that uses the query function. The functions.php includes the the require function also. However, I get message:
Fatal error: Call to a member function query() on a non-object...
If I instantiate the object within the function sendToDB it successfully writes to the DB. As soon as I take it out, I receive the same message. Is this a global issue? or am I best instantiating it everywhere I need it? Also if using $db on multiple pages, is it safe to run $db->close(); after finishing with it on each page?