Results 1 to 9 of 9

Thread: database connection class

  1. #1
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Question database connection class

    Hi guys,

    this is probably a really simple answer but what i'm looking for is a database connection class that will create a connection that can then be used (without passing it around) by other functions in other classes to query the database.

    I'm new to the whole class thing in php.

    Can anybody point me in the right direction or show me some snippets of code of how to do so?

    Cheers in advance.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  2. #2
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I would not use a whole class for that unless you needed it to have submitting\retriving functions. If anything just write a quick function that can be used (more helpful for multiple databases, but it also can get messy), or just straight out write mysql_connect(); at the top of your page (or functions page)
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I find it easiest to create a page called "conn.php" that connects to the database.

    Then at the top of every page:
    <?php
    include('path/to/conn.php');
    ....
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    I understand that it is probably easiest to just use a straight function to connect. OK if i go with the not OO approach, how would i be able to use the connection in another class?
    e.g.

    PHP Code:
    class Customer {

       var 
    $name;
       var 
    $email;

    function 
    getEmail () {
       return 
    $this->email;
    }

    function 
    setEmail ($value '') {
       
    $this->email $value;
    }

    //etc etc for getting & setting

    function readCustomer ($value '') {

       
    //i dont need to connect to the database because it has already been done in a standard function, but will the class need to know that i have?

       //$value will be an email address
       
    $sql "SELECT * FROM customer WHERE email = '$value'";
       
    $result mysql_query();
       
    $customer = new Customer();
       if(
    //results greater than 0) {
          
          
    $customer->setEmail //row from table
          
    $customer->setName //row from table

       
    } else {
          
    $customer->setEmail null;
          
    $customer->setName null;
       }

       return 
    $customer;
    }


    PHP Code:
    $conn mysql_connect(dbhostdbuserdbpass) or die ('There was an error in trying to connect to the database server. Please contact system administrator.');
    mysql_select_db(dbname);

    $customer Customer::read('test@test.com'); 
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  5. #5
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I dont know too much about classes, but you could try placing the connection script in the class before your query, at the top of the class page or in the page that it's being called.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    several ways:

    1) include the DB connection script at the beginning of the class you're using it in.
    PHP Code:
    <?php

    class needsDatabase{

    var 
    $DBconn;

    function 
    __construct(){

       include(
    'db_conn.php');  
       
    //  this script would assign the connection to some variable, say $db,
       //  which you could then use in the class
       
    $this->DBconn $db;
    }

    //  rest of class

    }
    ?>
    2) pass the connection into the object when you create it:
    PHP Code:
    <?php

    include('db_conn.php');  //  this gives you your connection ( $db )

    new needsDatabase($db);

    class 
    needsDatabase{

       var 
    $DBconn;

       function 
    __construct($DBconn){
          
    $this->DBconn $DBconn;
       }

       
    //  rest of class

    }

    ?>

    3)you could connect to the database at the beginning of your script and then declare the $db variable as global inside the class:
    PHP Code:
    <?php

    include('db_conn.php');  //  this gives you your connection ( $db )

    class needsDatabase{

       var 
    $DBconn;

       function 
    __construct(){
          
    $this->DBconn $GLOBALS['db'];
       }

      
    // rest of class

    }

    ?>
    number 2 is straightforward, but a little tedious in that you have to pass the variable that holds the connection each time. number 3 is the method I generally use, even though you have to make sure all of your classes refer to the correct global-scope variable (I just use the same variable name all the time). More about $GLOBALS
    Last edited by traq; 07-05-2010 at 07:38 PM.

  7. The Following User Says Thank You to traq For This Useful Post:

    city_coder (07-06-2010)

  8. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    First, I've never had problems with a database connection. Though $conn (or whatever name you use) may not be available as a variable within a class, I've never had scope issues when using a connection. This is because MySQL uses the last known connection (maybe at a deeper level than PHP), and this then means you connect once, then you are done. So just use an include at the top of each page, and that will set everything up and you don't have to worry again.
    The only case where this will get very complex is if you use two or more databases for anything. That will be when you need to worry about connections and scope.


    That said, I can't promise there won't be problems (though I haven't had any), so if you do need to work this out, then the methods above will help.

    Alternatively, here's an easy way to make this work:
    PHP Code:
    //in some file you include() within every page:
    function doDb() {
    return 
    $GLOBALS['conn'];
    }

    ////

    //now, in your class, or anywhere else:
    $conn doDb(); 
    Of course I'm not really sure that's any more helpful than traq's example above, but it is possible and gives you a little more control.

    ALSO, you could even within that force it to connect to the DB if it hasn't yet-- if (!isset($GLOBALS['conn'])).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. The Following 2 Users Say Thank You to djr33 For This Useful Post:

    city_coder (07-06-2010),traq (07-06-2010)

  10. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    ...I've never had scope issues when using a connection. This is because MySQL uses the last known connection...
    true. I hadn't thought of that.
    I usually use a DB class, so I was thinking in terms of being able to access that resource (not using the connection directly).

  11. #9
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Cheers guys, although i didnt want to pass any variables when i instantiate the class the methods you provided at least minimalise the impact to as small as possible.

    Much appreciated!
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •