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();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.