Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Database Wrapper
- * @version $Id$
- * @author Moazzam Khan <mkhan@talkpoint.com>
- * @package TPFramework
- */
- /**
- * Database wrapper (singleton)
- *
- * @author Moazzam Khan <mkhan@talkpoint.com>
- * @package TPFramework
- */
- class database {
- /** @var DB connection resource handle */
- private $_resource;
- /** @var string Internal variable to hold SQL */
- private $_sql;
- /** @var Internal variable to hold last query cursor */
- private $_cursor;
- /** @var Array Log of queries */
- private $_log = array();
- /** @var int Internal variable to hold the error number of last error */
- private $_errorNum;
- /** @var string Internal variable to hold the error number of last error */
- private $_errorMsg;
- /** @var debugging flag */
- public $debug = 0;
- /**
- * Constructor
- *
- * @param string $host
- * @param string $user
- * @param string $pass
- * @param string $name
- * @return database
- */
- private function database($host, $user, $pass, $name)
- {
- if (!($this->_resource = @mysql_pconnect($host, $user, $pass))) {
- trigger_error(mysql_error(), E_USER_ERROR);
- return;
- }
- if (!mysql_select_db($name)) {
- trigger_error("Database Selection failed", E_USER_ERROR);
- }
- }
- /**
- * @param string $host DB Hostname/IP
- * @param string $user DB Username
- * @param string $pass DB User's password
- * @param string $name DB Name
- * @return object
- */
- static function &getInstance($host='', $user='', $pass='', $name='')
- {
- static $db;
- if (!is_object($db))
- $db = new database($host, $user, $pass, $name);
- return $db;
- }
- function execute($sql)
- {
- if (!$this->query($sql)) return false;
- return true;
- }
- /**
- * @param string $sql SQL statement
- * @return Array of rows returned by the query
- */
- function getAll($sql)
- {
- $this->_sql = $sql;
- $ret =& $this->retrieveResults('', 0, 'assoc');
- return $ret;
- }
- /**
- * Returns an array of row objects
- * @param string $sql
- * @see retrieveResults
- */
- function loadObjectList($sql)
- {
- return $this->retrieveResults('', 0, 'object');
- }
- /**
- * Load an array of retrieved database objects or values
- * @param int Database cursor
- * @param string The field name of a primary key
- * @return array If <var>key</var> is empty as sequential list of returned records.
- * If <var>key</var> is not empty then the returned array is indexed by the value
- * the database key. Returns <var>null</var> if the query fails.
- */
- function &retrieveResults ($key='', $max=0, $result_type='row')
- {
- $results = array();
- $sql_function = 'mysql_fetch_'.$result_type;
- if ($cur = $this->query()) {
- while ($row = $sql_function($cur)) {
- if ($key != '') $results[$row->$key] = $row;
- else $results[] = $row;
- if ($max AND count($results) >= $max) break;
- }
- mysql_free_result($cur);
- }
- return $results;
- }
- /**
- * Execute the query
- * @return mixed A database resource if successful, FALSE if not.
- */
- function query($sql = '')
- {
- $this->_errorNum = 0;
- $this->_errorMsg = '';
- if ($sql == '') $sql = $this->_sql;
- if ($this->debug) $this->_log[] = $sql;
- if ($this->_cursor = mysql_query($sql, $this->_resource)) {
- $this->_errorNum = 0;
- $this->_errorMsg = '';
- return $this->_cursor;
- } else {
- $this->_errorNum = mysql_errno( $this->_resource );
- $this->_errorMsg = mysql_error( $this->_resource )." SQL=$sql";
- //if ($this->_debug) $this->debug_trace();
- return false;
- }
- }
- function errorMsg() {return $this->_errorMsg;}
- /** Closes the db connection */
- function close()
- {
- @mysql_close($this->_resource);
- $this->_resource = NULL;
- }
- }
Add Comment
Please, Sign In to add comment