«

»

Sep
04

Use a PHP class to connect to a database

Often times I’ve found myself wanting a quick and easy way to set up a connection to a MySQL database in PHP. In procedural code this was often accomplished by adding a few lines of code atop the first PHP file in the program.

I’ve decided to write an object that uses the constructor to connect to a database and the destructor to disconnect. All I need do now to connect to a database is update the class properties and instantiate the thing.

First Steps

I start by coding a few properties in my new class. These properties are the keys to connecting to the database.

Then I use the constructor method along with these settings to connect to a MySQL server and select a particular database. The destructor method disconnects from MySQL properly and will run when the PHP script shuts down at the end.

Limitations

The limitations of this script are it will only work where OOP and these methods are permitted, which is PHP 5. If you’re running an older version, I would recommend an upgrade, but you could create an instance using older methods and call a function to close your MySQL connection before the conclusion of the script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class DB {
 
	// These properties would reflect your
	// database's settings!
	private $db_host = 'localhost';
	private $db_user = 'myUser';
	private $db_pass = 'myPass';
	private $db_name = 'myDataBase';
	public  $db_base = null;
 
	// Now use the constructor to connect to the
	// database
	function __construct() {
		$this->db_base = mysql_connect(
			$this->db_host,
			$this->db_user,
			$this->db_pass
		);
		mysql_select_db($this->db_name);
	}
 
	// When the program stops, the destructor disconnects us
	function __destruct() {
		mysql_close($this->db_base);
	}
 
}

Once the class is written, all that remains is to include it in your main script and instantiate the class. Something like this would be valid:

1
2
3
4
5
6
7
8
9
10
11
12
// Include the class and instantiate it in our
// main script.
include(DB.class.php);
$db = new DB();
 
// Send a query to the database and it'll work...
// This will echo the value of the 'id' cell in the
// whole database.
$qry = mysql_query("SELECT * FROM table WHERE 1");
while($row = mysql_fetch_assoc($qry)) {
	echo $row['id'];
}

Conclusion

Hope this helps someone out there. You can now use this class to drag and drop database connectivity and only need to change the values of those three little fields in all of your PHP/MySQL projects.

This concept likely could be applied to other database systems as well. I’m just most familiar with MySQL so that’s why it’s my example.

1 comment

  1. Kaeden says:

    That’s raelly thinking out of the box. Thanks!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Before you post, please prove you are sentient.

what is 5 + 5?