#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0

    Tough question. Setting up modular code. Connections


    Howdeh.

    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
    Code:
        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.
    Code:
        require_once("assets/configs/connect.php"); 
        
        $conn->dbcon(); //Line 5
        
        class myclass {
    
        Function (some functions)
        {
    Results in :
    Code:
    > 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. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Cross-posted all around teh Internetz.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Jacques1
    xxxxxxxxxxx
    Wow, cyberstalking. What's your problem? I'm asking for help from a few different places. So what.

    Christ, I'm sorry I asked for help.

    Comments on this post

    • dmittner agrees : Some reputation for having to put up with Jacques. :)
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    connect.php
    Code:
    function connect(){
        $host = 'localhost';
        $username = 'x';
        $password = 'x';
        $dbname = 'x';
        return mysqli_connect($host , $username  , $password ,$dbname);
    }
    Code:
        require_once("assets/configs/connect.php"); 
        
        $conn = connect();
        
        class myclass {
    
        Function (some functions)
        {
    There are a lot of ways to take this in better directions but that'll put the DB connection into $conn for further use.
    LinkedIn: Dave Mittner
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by dmittner
    connect.php
    Code:
    function connect(){
        $host = 'localhost';
        $username = 'x';
        $password = 'x';
        $dbname = 'x';
        return mysqli_connect($host , $username  , $password ,$dbname);
    }
    Code:
        require_once("assets/configs/connect.php"); 
        
        $conn = connect();
        
        class myclass {
    
        Function (some functions)
        {
    There are a lot of ways to take this in better directions but that'll put the DB connection into $conn for further use.
    Thanks, there does seem to be a few ways to skin this cat, so to speak. I'd rather just define mysqli_connection within the class but manager tells me this is a no no.

    As for you know who, well I'm still waiting him to reveal my complete family history. Nothing yet though. What a disappointment.
  10. #6
  11. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by lakeshakeape
    Thanks, there does seem to be a few ways to skin this cat, so to speak. I'd rather just define mysqli_connection within the class but manager tells me this is a no no.
    I'd agree with him that it's better to establish the connection outside the class, but there are more elegant ways than using $conn as a global around your code.

    For instance:
    PHP Code:
    $connection connect();
    $vehicle = new Vehicle($connection); 
    or

    PHP Code:
    $connection connect();
    $vehicle = new Vehicle();
    $vehicle->setDB($connection); 
    The top one is utilizing Dependency Injection (DI) which is kind of the new OO rave. The latter is maybe a bit less common but a bit more flexible.

    I've also done similar to this:
    PHP Code:
    class Vehicle {
        public static 
    $db;
    }
    Vehicle::$db connect(); 
    But I think I'll be getting away from that practice in future development. It has the merit of being OO and being able to set a connection per class, but doesn't quite have the flexibility of DI that allows it to be set per object.
    LinkedIn: Dave Mittner

IMN logo majestic logo threadwatch logo seochat tools logo