Howdy. Perhaps I've being going about this all wrong, fair enough but essentially what I'm trying to do is place all my db connection info on another page connect.php
. The idea is to then setup a function (below) which can then be called in once on my class files for use within a global variable which can then be utilised by all the methods within the class.
Before we go further I want to add that I 'might' need to return an array of mysqli_connect(x.x.x.x) in my dbcon() function for the mysqli code to operate as intended but we'll get on to that later.
Here's what's going on so far. Had some help on this already so don't give me all the credit.
connect.php
function dbcon()
{
static $conn;
if (!$conn)
{
$host = 'localhost';
$username = 'x';
$password = 'x';
$dbname = 'x';
$conn = mysqli_connect($host , $username , $password ,$dbname);
}
return $conn;
}
On my class file this is how I am attempting to initiate it.
require_once("assets/configs/connect.php");
$conn->dbcon(); //Line 5
class myclass {
Function (some functions)
{
Results in :
Notice: Undefined variable: conn in C:\xampp\htdocs\cats\assets\includes\catclass.php on line 5
Fatal error: Call to a cat function dbcon() on a non-object in C:\xampp\htdocs\cats\assets\includes\catclass.php on line 5
Can anyone shed some light on what I'm doing wrong here, and crucially point me in the right direct to resolving this. Ideally, I don't want to have to back track on the approach I'm taking.