Monday, February 4, 2008

Simple PHP Database Connection Class

server  = $_DBINFO['server'];
$this->username = $_DBINFO['username'];
$this->password = $_DBINFO['password'];
$this->database = $_DBINFO['database'];
$this->connect();
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx CREATES A SERVER CONNECTION xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function connect(){
$this->conn = @mysql_connect($this->server, $this->username, $this->password);
if($this->conn == FALSE){
echo 'Cannot connect to server \''.$this->server.'\'.'; exit;
}
$dbconnect = @mysql_select_db($this->database, $this->conn);
if($dbconnect == FALSE){
echo 'Unable to connect to database \''.$this->database.'\''; exit;
}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx PROCESS SQL QUERY xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function query($sql){
$this->query = @mysql_query($sql, $this->conn) or print_r(mysql_error());
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx QUERY RESULT IN ARRAY FORM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function getRecord(){
$resultCount = mysql_num_rows($this->query);

if ($resultCount==1){
$result = @mysql_fetch_assoc($this->query);
}
else if ($resultCount > 1){
while($row = @mysql_fetch_assoc($this->query)){
$result[]=$row;
}
}
else{
$result = NULL;
}
return $result;
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx GET TOTAL RESULT COUNT xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function query_count(){
return @mysql_num_rows($this->query);
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx GET LAST INSERTED ID xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function last_insert_id(){
return @mysql_insert_id($this->conn);
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx RETURN ONLY ONE SPECIFIC FIELD xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function getField($fieldname){
while($row = @mysql_fetch_assoc($this->query)){
$result[]=$row[$fieldname];
}
return $result;
}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxx HOW-TO-USE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// A simple demo on how to use this module
// DIRECTION: download file and save then remove the ".txt" extension to be saved as a php file
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/*
// ASSIGN DATABASE SERVER INFO INTO ARRAY VALUES
$serverInfo['server'] = 'localhost';
$serverInfo['username'] = 'root';
$serverInfo['password'] = '';
$serverInfo['database'] = 'rednixchat';

// CREATE NEW Database OBJECT
$db = new Database($serverInfo)

// CREATE AN SQL STATEMENT
$sqlSelect = "SELECT * FROM user_info";

// PERFORM SQL STATEMENT ASSIGNED IN $sqlSelect
$db->query($sqlSelect);

// GET TOTAL RESULT FOR SQL QUERY
$totalResult = $db->query_count();

// GET QUERY RESULT IN ARRAY FORM
$queryResult = $db->getRecord();

// DISPLAY QUERY RESULT
print_r($queryResult);
*/
?>

2 comments:

Romeo John Pasion said...

Wow that's a nice pice of code thanx a lot i think im gonna try that.

GringO . said...

Isn't there something missing at the start of the post?