0

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.

2
Contributors
1
Reply
14
Views
4 Years
Discussion Span
Last Post by paulkd
0

Yes you are doing it wrong.
You are calling $conn like a class but you have not declared a class in your connect.php file.

You should google for beginners php oop.

Here's an oop mysqli link

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.