View Single Post
Old Dec 2nd, 2004, 4:24 PM   #1
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Hey,

i made this to save some typing when connecting to a postgresql database. I havn't updated the code in a while, so there is probally some things that could be improved.

dbobj.php
[php]

///////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This file is an object that handles any and all db operations that you may //
// need. //
//Comments: If your going to change any of this code, please send a copy to: //
// mutherfucker@crcw.mb.ca //
// or //
// pizentios@gmail.com //
// Date Created: 04/20/04 //
// Date Finished: 04/21/04 //
///////////////////////////////////////////////////////////////////////////////////////////

Class dbobj {

//////////////////////////////////////////////////////////////////////////////////////////
//************************************Varibles******************************************//
//////////////////////////////////////////////////////////////////////////////////////////

var $user;
var $dbname;
var $pass;
var $connection;
/////////////////////////////////////////////////////////////////////////////////////////
//***********************************Properties****************************************//
/////////////////////////////////////////////////////////////////////////////////////////

function setUser($u)
{
//////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you set the user name that you are going to be //
// connecting as. //
// Pre: You need to send me the user name that you would like to set $user too. //
// Post: Returns nothing, but it does set the user var to the user name that you//
// wish. //
// Date Created: 04/20/04 //
// Date Finished: 04/20/04 //
//////////////////////////////////////////////////////////////////////////////////
$this->user=$u;
}

function getUser()
{
//////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you see what user you are connecting to the db as//
// Pre: The user var has to be set. //
// Post: Returns the user name. //
// Date Created: 04/20/04 //
// Date Finished: 04/20/04 //
//////////////////////////////////////////////////////////////////////////////////

return $this->user;
}

function setDBname($name)
{
//////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you set the database name that you want to //
// connect too. //
// Pre: You need to send a dbname. //
// Post: Sets the dbname var to what you send to me. //
// Date Created: 04/20/04 //
// Date Finished: 04/20/04 //
//////////////////////////////////////////////////////////////////////////////////
$this->dbname=$name;

}

function getDBname()
{
//////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you grab the dbname that you set. //
// Pre: Nothing needs to be sent, although it will return nothing if the dbname //
// hasn't been set. //
// Post: Returns the dbname var. //
// Date Created: 04/20/04 //
// Date Finished: 04/20/04 //
//////////////////////////////////////////////////////////////////////////////////
return $this->dbname;

}

function setPass($p)
{
//////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you set the password that you want to use to //
// connect to the db. //
// Pre: You need to send the password that you want to set to me. //
// Post: Returns nothing, however it does set the password to the pass var. //
// Date Created: 04/20/04 //
// Date Finished: 04/20/04 //
//////////////////////////////////////////////////////////////////////////////////
$this->pass=$p;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
//*********************************************Methods***************************************************//
///////////////////////////////////////////////////////////////////////////////////////////////////////////


function conn()
{
////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function makes the connection to the db that you have set up for //
// Pre: Nothing needs to be sent here, although everything needs to be set in //
// order for this function to complete it's task. //
// Post: Returns True if the connection is good. False if it is not. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
////////////////////////////////////////////////////////////////////////////////////

if ($this->pass <> "")
{
$constr = "dbname={$this->dbname} user={$this->user} pass={$this->pass}";
}
else
{
$constr = "dbname={$this->dbname} user={$this->user}";
}
$this->connection = pg_connect($constr);

if ($this->connection <> false)
{
return true;
}
else
{
return false;
}
}

function dbSelect($what, $from, $where)
{
/////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you do a select from the db that you just connected //
// too. //
// Pre: The connection has to be made, also you need to send at least the what //
// and from vars for this function to run. //
// Example: if you wanted to select all cats from the table called cats: //
// dbSelect("*", "cats", ""); //
// if you wanted to select all the cats from the table called cats //
// that are red: //
// dbSelect("*", "cats", "color=red"); //
// Post: Returns the record set. Returns false if it blew up. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
/////////////////////////////////////////////////////////////////////////////////////

if ($where == "")
{
$sql = "SELECT {$what} FROM {$from}";
}
else
{
$sql = "SELECT {$what} FROM {$from} WHERE {$where}";
}
$res = pg_query($sql);

if ($res <> false)
{
return $res;
}
else
{
return false;
}
}

function dbInsert($into, $colums, $values, $where)
{
///////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you do a insert into the selected db. //
// Pre: you need to have a connection and send the following items: //
// $into: the name of the table that you want to insert into. //
// $colums: The name of the colums that you want to insert the data into. Send a//
// "" if you don't need to spesifiy the colums that you want to insert //
// into. //
// $values: The actual values that you are wanting to insert. //
// $where: The where clause of a insert. Send a "" if you don't need to do a //
// where clause. //
// Exmaple: dbInsert("cats", "", "'brown', 'big', 'green'", "Size='small'"); //
// to insert into table cats the values brown, big, green where the //
// size is = small. //
// Post: Returns true if the insert was a sucess. False if it failed. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
///////////////////////////////////////////////////////////////////////////////////////

if ($colums <> "" AND $where <> "")
{
$sql = "INSERT INTO " . $into . " (" . $colums . ") VALUES (" . $values . ") WHERE " . $where . ";";
}
elseif ($colums <> "" AND $where == "")
{
$sql = "INSERT INTO " . $into . " (" . $colums . ") VALUES (" . $values . ");";
}
elseif ($colums == "" AND $where <> "")
{
$sql = "INSERT INTO " . $into . " VALUES (" . $values . ") WHERE " . $where . ";";
}
else
{
$sql = "INSERT INTO " . $into . " VALUES (" . $values . ");";
}
$res = pg_query($this->connection, $sql);

if ($res <> false)
{
return true;
}
else
{
return false;
}
}

function dbUpdate($table, $set, $where)
{
/////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you update a row of data. //
// Pre: You need to send the table that you want to update, the colums and the values //
// that you would like to set, and a where clause. If you don't want to use a //
// where clause just go like this "". //
// Post: Updates a row(s) of data. Returns true if it worked, false if it didn't. //
// Date Created: 05/06/04 //
// Date Finished: 05/06/04 //
/////////////////////////////////////////////////////////////////////////////////////////
if ($where <> "")
{
//then we are using a where clause.
$sql = "UPDATE " . $table . " SET " . $set . " WHERE " . $where;
}
else
{
//then we are not using the where clause.
$sql = "UPDATE " . $table . " SET " . $set;
}
$res = pg_query($this->connection, $sql);

if ($res <> false)
{
return true;
}
else
{
return false;
}
}


function dbDelete($table, $where)
{
//////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you delete row(s) out of a table. //
// Pre: you need to have the connection set up and you need to send the table that you //
// want to delete out of. WARNING: If you send a "" as where then it'll delete //
// everything out of the table. you need to send somthing in the where if you want //
// to prevent this, unless that's what your trying to do. //
// Post: Returns True if it worked, false if it didn't. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
//////////////////////////////////////////////////////////////////////////////////////////
if ($where <> "")
{
$sql = "DELETE FROM " . $table . " WHERE " . $where . ";";
}
else
{
$sql = "DELETE FROM " . $table . ";";
}

$res = pg_query($this->connection, $sql);

if ($res <> false)
{
return true;
}
else
{
return false;
}
}

function dbDrop($table)
{
////////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you drop a table. //
// Pre: You need a connection and you need to send the name of the table that you want to //
// delete...WARNING: this cannot be undone. //
// Post: Returns true if it worked, false if it didn't. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
////////////////////////////////////////////////////////////////////////////////////////////

$sql = "DROP TABLE ". $table . ";";

$res = pg_query($this->connection, $sql);

if ($res <> false)
{
return true;
}
else
{
return false;
}
}

function anysql($sql)
{
/////////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function lets you run any other sql that you might need to run. //
// Pre: You need a connection, and you need to send a FULL sql string. //
// Post: Returns the results if it worked. returns false if it didn't. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
/////////////////////////////////////////////////////////////////////////////////////////////
$res = pg_query($this->connection, $sql);

if ($res <> false)
{
return $res;
}
else
{
return false;
}
}

function connClose()
{
///////////////////////////////////////////////////////////////////////////////////////////////////
// Purpose: This function closes the conenction. //
// Pre: The connection has to been made. //
// Post: Returns true if it worked, false if it didn't. //
// Date Created: 04/21/04 //
// Date Finished: 04/21/04 //
///////////////////////////////////////////////////////////////////////////////////////////////////
$res = pg_close($this->connection);

if ($res <> false)
{
return true;
}
else
{
return false;
}
}
}

?>
[/php]

Here's an example on how to do a select with the object.

[php]
$db = new dbobj();
$db->setUser("postgres");
$db->setDBname("mydata");
$cats = array();
if($db->conn())
{
$res = $db->dbSelect("name", "cats", "color='blue'");
if ($res)
{
//loop through and dump the data into a array
for ($x=0; $x
{
$data = pg_fetch_array($res, $x);
$cats[$x] = $data["name"];
}
}
else
{
//query error message here.
}
}
else
{
//db error message here.
}
?>
[/php]

So that would return all the cats "name" that are of the color blue. Then you could loops through the cats array and output the names, or you could output them strait to the screen inside the for loop. i usally use pg_fetch_object() but for this example i desided to use pg_fetch_array(). Anyways, like a said before, this code probally could be cleaner since it was written a while ago.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!

Last edited by big_k105; Feb 11th, 2005 at 10:04 AM.
Pizentios is offline   Reply With Quote