If you develop websites in PHP5 or later, you know to the 'new stuff' in PHP5 called OOP, this is a little guide to create a class to your website, so you can connect to your mysql database the OOP way.
First we create a class called "db".
CODE
<?PHP
class db
{
}
?>
We need some information to your database, so we can connect to the right one, we need information for the Hostname, Database name, username and password to the mysql database.
So now we create some variables to store the information:
CODE
<?PHP
class db
{
private $hostname;
private $username;
private $password;
private $database;
private $connect;
private $select_db;
}
Before we can use the variables we need to set the information in the class contructor so the content of the variables is set every time we use the class.
CODE
public function db()
{
$this->hostname = "Write your hostname here";
$this->username = "Write your username here";
$this->password = "Write your password here";
$this->database = "Write your database name here";
}
Here you can see where to place the constructor:
CODE
<?PHP
class db
{
private $hostname;
private $username;
private $password;
private $database;
private $connect;
private $select_db;
public function db()
{
$this->hostname = "Write your hostname here";
$this->username = "Write your username here";
$this->password = "Write your password here";
$this->database = "Write your database name here";
}
}
The constructor runs every time you create an object of the class.
Now we can start to write our first function, the function that opens the connection to the database.
CODE
public function open_connection()
{
try
{
$this->connect = mysql_connect($this->hostname,$this->username,$this->password);
$this->select_db = mysql_select_db($this->database);
}
catch(exception $e)
{
return $e;
}
}
The function is placed under the constructor, but what does it do ?
the open_connection function trys to call the mysql_connect function and places the new conenction in the variable $this->connect, after that, it trys to select the right database and places the selected database in the variable $this->select_db, if the two trys returns any errors, then the function runs the Catch and returns the error message to the user.
Now our class look like:
CODE
<?PHP
class db
{
private $hostname;
private $username;
private $password;
private $database;
private $connect;
private $select_db;
public function db()
{
$this->hostname = "Write your hostname here";
$this->username = "Write your username here";
$this->password = "Write your password here";
$this->database = "Write your database name here";
}
public function open_connection()
{
try
{
$this->connect = mysql_connect($this->hostname,$this->username,$this->password);
$this->select_db = mysql_select_db($this->database);
}
catch(exception $e)
{
return $e;
}
}
}
But, now the conenction is open, how about closing it ?
So now we create an function to close the connection, we can't let the database be open right.
CODE
public function close_connection()
{
try
{
mysql_close($this->connect);
}
catch(exception $e)
{
return $e;
}
}
We can place the function under the function thats opens the connection.
CODE
<?PHP
class db
{
private $hostname;
private $username;
private $password;
private $database;
private $connect;
private $select_db;
public function db()
{
$this->hostname = "Write your hostname here";
$this->username = "Write your username here";
$this->password = "Write your password here";
$this->database = "Write your database name here";
}
public function open_connection()
{
try
{
$this->connect = mysql_connect($this->hostname,$this->username,$this->password);
$this->select_db = mysql_select_db($this->database);
}
catch(exception $e)
{
return $e;
}
}
public function close_connection()
{
try
{
mysql_close($this->connect);
}
catch(exception $e)
{
return $e;
}
}
}
Then, now its time to use our db class to something usefull, but how do we do that ?
First of all, we need to create some kind of function to handle all our queries to and from the database, so we don't need to rewrite it all the time, remember that an website uses a lot of queries to get all the content.
Lets start with the funtion to run queries.
CODE
public function query($sql)
{
try
{
$this->open_connection();
$sql = mysql_query($sql);
}
catch(exception $e)
{
return $e;
}
$this->close_connection();
return $sql;
}
And what do that function do ?
The function opens the connection to the database and trys to run the query that the user writes, and returns the result to the user, so he or she can do whatever its needed to make the results readable for the viewer of the website.
Lets place it in the class:
CODE
<?PHP
class db
{
private $hostname;
private $username;
private $password;
private $database;
private $connect;
private $select_db;
public function db()
{
$this->hostname = "Write your hostname here";
$this->username = "Write your username here";
$this->password = "Write your password here";
$this->database = "Write your database name here";
}
public function open_connection()
{
try
{
$this->connect = mysql_connect($this->hostname,$this->username,$this->password);
$this->select_db = mysql_select_db($this->database);
}
catch(exception $e)
{
return $e;
}
}
public function close_connection()
{
try
{
mysql_close($this->connect);
}
catch(exception $e)
{
return $e;
}
}
public function query($sql)
{
try
{
$this->open_connection();
$sql = mysql_query($sql);
}
catch(exception $e)
{
return $e;
}
$this->close_connection();
return $sql;
}
}
Now, we can start to use the class on our website, in the top of the index page, we starts by including the class, i have placed the class in a file called db.php.
CODE
<?PHP
include('db.php');
?>
But thats not all, we need to initialize the class to use the functions.
CODE
<?PHP
include('db.php');
$db = new db();
?>
NOW we are ready to rock the web, lets make a simple test, to be sure that the class works.
CODE
<?PHP
include('db.php');
$db = new db();
$db->query("SELECT data FROM table WHERE something = 'something'");
if($sql)
{
while($r = mysql_fetch_array($sql))
{
echo $r['data'];
}
}
?>
This code should not work at all on your site, but gives you an idea of how you can use the class.
I hope that you can use this little guide to OOP Database connections.