Let's assume I have 2 php files
In the first one I have the connection to the database. Since I need it in another php files, I assigned it to a global variable like this.
setconnection.php
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'database';
// Create connection
$GLOBALS['connect'] = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($GLOBALS['connect']->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected=mysqli_select_db($GLOBALS['connect'],'database');
mysqli_set_charset($GLOBALS['connect'],'utf8');
?>
And then I have another php file in which I use the connection from the above php file.
It is correct to close the connection like this?
index.php
include_once "setconnection.php";
$GLOBALS['connect']->close();