<?php
////////////////////////////////////////////////////
// static class DBConnect
//
// Static Database Connection Handler Class,
// essentially it aliases mysql_* functions from
// php.
//
// Author: Andy Honeycutt
// Email: hide@address.com
// Date: 2008-09-04
//
// This program is free software; you can redistribute it
// and/or modify it under the terms of the
// GNU General Public License version 2 (GPLv2)
// as published by the Free Software Foundation.
// See COPYING for details.
//
///////////////////////////////////////////////////////
class DBConnect
{
// The hostname for our database server.
// @var string
static public $HOST;
// The user which has access to our database.
// @var string
static public $USER;
// The password for our database user.
// @var string
static public $PASS;
// The database (schema) we wish to use.
// @var string
static public $BASE;
// Our [eventual] database link.
// @var mixed
static private $LINK = -1;
/**
* Set our host, user, password, and database schema
* in one go.
*
* @param string $h hostname that is our database host
* @param string $u username to connect to our database
* @param string $p password for our database user
* @param string $b the name of our database schema
*/
public function Prepare($h,$u,$p,$b)
{
self::setHost($h);
self::setUser($u);
self::setPass($p);
self::setBase($b);
}
// Set the host for our database connector.
// @param string $host
public function setHost($h) { self::$HOST = $h; }
// Set the user for our database connection.
// @param string $username
public function setUser($u) { self::$USER = $u; }
// Set the password for our database user.
// @param string $password
public function setPass($p) { self::$PASS = $p; }
// Set the name of our database schema.
// @param string $database
public function setBase($b) { self::$BASE = $b; }
// Assign our link to an object.
// @param object $l
private function setLink($l) { self::$LINK = $l; }
// Get the host of our database connector.
// @return string
public function getHost() { return self::$HOST; }
// Get the username of our database connection.
// @return string
public function getUser() { return self::$USER; }
// Get the password for our database user.
// @return string
public function getPass() { return self::$PASS; }
// Get the name of the schame for our database.
// @return string
public function getBase() { return self::$BASE; }
// Get the link object for our database connection.
// Function getLink() returns the current link, or
// creates a link if one has not yet been established.
// @return object
public function getLink()
{
// Check to see if we already have a link established.
if( self::$LINK != -1 )
{
// return our database link.
return self::$LINK;
}
else
{
// if we haven't already created a database connection,
// attempt to create one here.
try
{
// try to connect using our connection info, set our link
// to the new connection.
$link = mysql_connect(self::getHost(),self::getUser(),self::getPass());
self::setLink($link);
}
catch(Exception $e)
{
echo "Could not establish a link to the specified database.\n" . $e->getMessage();
self::setLink(-1);
}
// return our newly created database link.
return self::$LINK;
}
}
// Select the database schema we wish to work with. If
// $db is omitted, we will use the schema name provided
// earlier (assumes self::setBase($b) was called earlier).
// @param string $db
public function SelectDb($db='')
{
try
{
if( $db == '' )
mysql_select_db(self::getBase(),self::getLink());
else
mysql_select_db($db,self::getLink());
}
catch(Exception $e)
{
echo "Could not select the specified database '$db'.\n" . $e->getMessage();
}
}
// Perform a select query using query $q.
// @param string $q (Our query)
// @return object (result set)
public function Select($q)
{
try
{
$result = mysql_query($q,self::getLink());
if( is_resource($result) )
return $result;
}
catch(Exception $e)
{
echo "Could not query datasource.\n" . $e->getMessage();
}
return 0;
}
// Perform an insert query using query $q
// @param string $q (Query)
// @return int record insert id
public function Insert($q)
{
try
{
mysql_query($q,self::getLink());
return mysql_insert_id(self::getLink());
}
catch( Exception $e )
{
echo "Bad insert: " . $e->getMessage();
return -1;
}
}
// Perform an update query using query $q.
// @param string $q (Query)
// @return value (Anything mysql_query returns.)
public function Update($q)
{
return mysql_query($q,self::getLink());
}
// Perform a delete query using query $q.
// @param string $q (Query)
// @return value (Anything mysql_query returns.)
public function Delete($q)
{
return mysql_query($q,self::getLink());
}
// Get the next row from our resultset $r.
// @param object $r (Resource)
// @return array
public function getRow($r)
{
if( is_resource($r) ) return mysql_fetch_assoc($r);
return null;
}
// Closes our database connection if the link
// is still active.
public function Close()
{
if( is_resource(self::$LINK) ) mysql_close(self::$LINK);
}
// mysql_real_escape_string alias
public function Escape($s)
{
return mysql_real_escape_string($s,self::$LINK);
}
}
/**
* Sample usage of DBConnect Static Class
*/
/**
* Configure our Database Connection
*/
//DBConnect::setHost("127.0.0.1");
//DBConnect::setUser("myUser");
//DBConnect::setPass("myPass");
//DBConnect::setBase("mySchema");
//
///** Or **/
//
//DBConnect::Prepare("127.0.0.1","myUser","myPass","mySchema");
//
///** Perform a delete and an update **/
//DBConnect::Delete("DELETE FROM myTable WHERE ID=1");
//DBConnect::Update("UPDATE myTable SET column='abc' WHERE ID=1");
//
///** Perform a query **/
//$mysql_result = DBConnect::Select("SELECT * FROM myTable");
//
///** Loop through results **/
//
//while($row = DBConnect::getRow($mysql_result))
// print_r($row);
//
///** Close our connection **/
//DBConnect::Close();