Jump to content

How do i make the code below object oriented ?


jd2007

Recommended Posts

Why do you need to?

 

<?php
class database {

// Contains host
private $ho;
// Contains database name
private $dn;
// Contains database user
private $du;
// Contains user password
private $up;

public function __construct($ho, $dn, $du, $up) {
   // Sets the connection information
	$this->ho = $ho;
	$this->dn = $dn;
	$this->du = $du;
	$this->up = $up; 
}


public function connect() {
// Establishes a mysql connection and selects the database
	if(!$this->connection = mysql_connect($this->ho, $this->du, $this->up)) {
	    // If we fail, tell the class that we have failed!
	    throw new exception('MySQL Error :'.mysql_errno().' - Connection to mysql server could not be established -- '.mysql_error(), 1);
	}
	if(!mysql_select_db($this->dn,$this->connection)){
	    // If we fail to select the database, tell the class that we have done so!
	    throw new exception('MySQL Error :'.mysql_errno().' - Database could not be selected -- '.mysql_error(), 1);
	}
	return true;
}

public function disconnect() {
// Disconnects from the mysql server.
	return mysql_close($this->connection);
}

}

// TO USE:
$DBConnection = new database('localhost', 'messenger', 'yourusername', 'yourpassword');
$DBConnection->connect();

// Your db queries go here

$DBConnection->disconnect();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.