I have tried putting a mysqli connection script into a php function that various php files can call to connect to the database. I have created the following function:
public function connectToDatabase() {
$con = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
echo "<br><br>";
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL:" . mysqli_connect_error();
} else {
echo "Connection successful";
}
return $con;
}
I then call that function from another file using (the above file has been included):
$con = connectToDatabase();
However, while the code in the top function works fine, passing the connection in the variable $con doesnt seem to work. It has occured to me that the connection is closing automatically when it reaches the return statement. Is this the case? If so, how can I stop that?
Many thanks