Log in

View Full Version : database connection class



city_coder
07-03-2010, 11:21 AM
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.

fileserverdirect
07-04-2010, 10:23 PM
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)

djr33
07-05-2010, 06:01 AM
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');
....

city_coder
07-05-2010, 08:24 AM
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.



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;
}

}




$conn = mysql_connect(dbhost, dbuser, dbpass) 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');

fileserverdirect
07-05-2010, 06:30 PM
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.

traq
07-05-2010, 07:32 PM
several ways:

1) include the DB connection script at the beginning of the class you're using it in.
<?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

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

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 (http://php.net/manual/en/reserved.variables.globals.php)

djr33
07-06-2010, 01:47 AM
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:

//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'])).

traq
07-06-2010, 03:00 AM
...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).

city_coder
07-06-2010, 07:39 AM
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! :D