PHP Classes

File: classes/mysqlez.php

Recommend this page to a friend!
  Classes of Dwaine Halberg   mySQLEz   classes/mysqlez.php   Download  
File: classes/mysqlez.php
Role: Class source
Content type: text/plain
Description: mySQLEz Class
Class: mySQLEz
MySQL database access wrapper
Author: By
Last change:
Date: 13 years ago
Size: 15,935 bytes
 

Contents

Class file image Download
<?php //deny direct access to included files if( basename( __FILE__ ) == basename( $_SERVER['PHP_SELF'] ) ) { exit(); } /** * @file name: mysqlez.php * * @creation date: 06/08/2010 * * @author: Dwaine Halberg * * @company: Autumn Hail * * @file ver 1.5 * * @rev date: 10/07/2011 * * @desc: * mySQLEz db abstraction class * makes it easy to perform * crud to a mySQL Database. * * @license: dbad (don't be a dick!) * http://dbad-license.org/ * otherwise modifiy away and enjoy!! * **/ /** * @class: mySQLEz class * * @desc: Easy MySQL Database abstraction layer * * @methods: total = 17 * __construct() * getInstance() * __clone() * dbConnect() * dbClose() * dbFreeResult() * dbEscape() * dbQueryOrDie() * doSQL() * doInsert() * doUpdate() * doDelete() * fetchObj() * fetchArray() * fetchRow() * fetchCount() * varDump() * **/ class mySQLEz { /** * @class vars * * @desc: defines vars base states * **/ protected $dbUser = ''; protected $dbPass = ''; protected $dbHost = ''; protected $dbName = ''; protected $queryResult = NULL; private $lastError = NULL; private $lastQuery = NULL; private $dbo = NULL; private static $instance = NULL; /** * @const: DB_CLASS_VER * * @desc: * Class version constant for Exceptions * * @params" * DB_CLASS_VER = "current version" * **/ const DB_CLASS_VER = '1.5'; /** * @method: function __construct() * * @desc: Class Constructor * * #params: $dbHost: string * $dbUser: string * $dbPass: string * $dbName: string * * @return: @dbo **/ function __construct( $dbHost,$dbUser,$dbPass,$dbName ) { $this->dbUser = $dbUser; $this->dbPass = $dbPass; $this->dbName = $dbName; $this->dbHost = $dbHost; $this->dbConnect(); return $this->dbo; } /** * @method: get Instance * * #desc: the single instance * * @return: Singleton */ public static function getInstance() { if( self::$instance ) { return self::$instance; } return self::$instance = new self(); } /** * @method: PHP Magic Method clone * * @desc: Disallow cloning * */ private function __clone(){} /** * @method: public function dbConnect() * * @desc: provides the database connection for the class * * @params: $dbHost: string * $dbUser: string * $dbPass: string * $dbName: string * * @return: boolean: status **/ public function dbConnect() { try { if( !$this->dbo = mysql_pconnect( $this->dbHost, $this->dbUser, $this->dbPass ) ) { throw new Exception( '<div><strong>Error: mySQLEz ( v '. self::DB_CLASS_VER .' ) could not connect to database, check the host "'.$this->dbHost.'", user "'.$this->dbUser.'", db "'.$this->dbName.'", and password "******" are correct.</strong></div>' ); } if( !mysql_select_db( $this->dbName, $this->dbo ) ) { throw new Exception( '<div><strong>Error: mySQLEz ( v '. self::DB_CLASS_VER .' ) could not select database, check the database name is correct "'.$this->dbName.'".</strong></div>' ); } } catch( Exception $e ) { die( $e->getMessage() ); } } /** * @method: public function dbClose() * * @desc: closes active database connection * * @params: void * * @return: boolean: status **/ public function dbClose() { return mysql_close( $this->dbo ); } /** * @method: private function dbFreeResult() * * @desc: free results to conserve memory * * @params: $query * * @return: boolean: status **/ private function dbFreeResult( $query ) { return mysql_free_result( $query ); } /** * @method: flush * * @desc: * Delete cached queries * **/ private function flush() { /** dump this **/ $this->lastResult = NULL; $this->lastQuery = NULL; } /** * @method: public function dbEscape() * * @desc: escapes data for safe insert * * @param: * $str - string * $like - string * * @return: string **/ public function dbEscape( $str, $like = FALSE ) { if( !ini_get( 'magic_quotes_gpc' ) ) { if( is_array( $str ) ) { foreach ( $str as $key => $val ) { $str[$key] = $this->dbEscape( $val, $like ); } return $str; } if( function_exists('mysql_real_escape_string' ) ) { $str = mysql_real_escape_string( $str ); } elseif( function_exists( 'mysql_escape_string' ) ) { $str = mysql_escape_string( $str ); } else { $str = addslashes( $str ); } // escape LIKE conditions if( $like === TRUE ) { $str = str_replace( array( '%', '_' ), array( '\\%', '\\_' ), $str ); } return $str; } } /** * @method: private function queryOrDie() * * @desc: private function todo queries against * * @params: * $query - string mixed * * @return $sql results or error! **/ private function queryOrDie( $query ) { /** flush cached values **/ $this->flush(); /** keep the last query for debug **/ $this->lastQuery = $query; $queryResult = mysql_query( $query, $this->dbo ); try { if( !$queryResult ) { throw new Exception( '<div><strong>Error: There is problem with your query statement:</strong><br /><h4>"'.$query.'"</h4></div>' ); } else { return $queryResult; $this->dbFreeResult( $queryResult ); } } catch( Exception $e ) { echo $e->getMessage(); } } /** * @method: public function doSQL() * * @desc: user defined sql queries * * @params: * $sql - string * * @return $results - array **/ public function doSQL( $sql ) { /** log how the function was called **/ $this->funcCall = "\$db->doSQL( \"$queryOrDie\", $output )"; $results = $this->dbEscape( $this->queryOrDie( $sql ) ); if( $results == '' && mysql_affected_rows() != -1 ) { return FALSE; } return $results; } /** * @method: public function doInsert() * * @desc: takes care of insert queries * * @params: * $table - string * $postVals - post array mixed * * @return boolean **/ public function doInsert( $table, $postVals ) { /** log how the function was called **/ $this->funcCall = "\$db->doInsert( \"$queryOrDie\", $output )"; //loop through post values and create sql statement foreach( $postVals as $key=>$value ) { $newRow[] = $key.'=\''.$value.'\''; } $newRow = implode( ',', $newRow ); $this->dbEscape( $sql = "INSERT INTO $table SET $newRow" ); $result = $this->queryOrDie( $sql ); //check to see if the update was successful or not if( $result =='' && mysql_affected_rows() != -1 ) { return FALSE; } return TRUE; } /** * @method: public function doUpdate() * * @desc: takes care of update queries * * @params: * $table - string * $keyColName - string * $id - mixed * $postVals - post array - mixed * * @return boolean **/ public function doUpdate( $table, $keyColName, $id, $postVals ) { /** log how the function was called **/ $this->funcCall = "\$db->doUpdate( \"$queryOrDie\", $output )"; //loop through post values and create sql statement foreach( $postVals as $key=>$value ) { $newRow[] = $key.'=\''.$value.'\''; } $newRow = implode( ',', $newRow ); $this->dbEscape( $sql = "UPDATE $table SET $newRow WHERE $keyColName = '$id'" ); $result = $this->queryOrDie( $sql ); //check to see if the update was successful or not if( $result =='' && mysql_affected_rows() != -1 ) { return FALSE; } return TRUE; } /** * @method: public function dodelete() * * @desc: takes care of delete queries * * @params: * $table - string * $id - int * * @return: boolean **/ public function doDelete( $table, $id ) { /** log how the function was called **/ $this->funcCall = "\$db->dodelete( \"$queryOrDie\", $output )"; $this->dbEscape( $sql = "DELETE FROM $table WHERE id = (int)$id" ); $result = $this->queryOrDie( $sql ); if( !$result ) { return FALSE; } return TRUE; } /** * @method: public function fetchObj() * * @desc: queries tables for full result sets with sorts * * @params: * $sql - string mixed * * @return: object **/ public function fetchObj( $sql ) { /** log how the function was called **/ $this->funcCall = "\$db->fetchObj( \"$queryOrDie\", $output )"; $this->dbEscape( $r = $this->queryOrDie( $sql ) ); $results = array(); while( $rows = @mysql_fetch_object( $r ) ) { $results[] = $rows; } if( !$results ) { return FALSE; } return $results; } /** * @method: public function fetchAssoc() * * @desc: queries tables for full result sets with sorts * * @params: * $sql - string mixed * * @return: array MYSQL_NUM, MYSQL_ASSOC, MYSQL_BOTH **/ public function fetchArray( $sql, $arrayType ) { /** log how the function was called **/ $this->funcCall = "\$db->fetchArray( \"$queryOrDie\", $output )"; $this->dbEscape( $r = $this->queryOrDie( $sql ) ); while( $rows = @mysql_fetch_array( $r, $arrayType ) ) { $results[] = $rows; } if( !$results ) { return FALSE; } return $results; } /** * @method: public function fetchRow() * * @desc: queries tables for row result sets by id * * @params: * $table - string * $fields - mixed * $keyColName - string * $id - int * * @return: single row of results as objects **/ public function fetchRow( $table, $fields='*', $keyColName, $id ) { /** log how the function was called **/ $this->funcCall = "\$db->fetchRow( \"$queryOrDie\", $output )"; $this->dbEscape( $sql = "SELECT $fields FROM $table WHERE $keyColName = '$id' LIMIT 1" ); $r = $this->queryOrDie( $sql ); $results = array(); while( $row = @mysql_fetch_object( $r ) ) { $results[] = $row; } if( !$results ) { return FALSE; } return $results; } /** * @method: public function fetchCount() * * @desc: queries tables for row result sets by id * wiht options where for tables with complex sorts * * @params: * $table - string * $fields - string * $keyColName - string * $id - int * * @return: results count **/ public function fetchCount( $table, $fields='*', $keyColName, $id ) { /** log how the function was called **/ $this->funcCall = "\$db->fetchCount( \"$queryOrDie\", $output )"; $whereSql = ''; if( $keyColName != '' ) $whereSql = "WHERE $keyColName = '$id'"; $this->dbEscape( $sql = "SELECT $fields FROM $table " . $whereSql ); $r = $this->queryOrDie( $sql ); $count = @mysql_num_rows( $r ); if( !$count ) { return FALSE; } return $count; } /** * @method: varDump * * @desc: * does a dump of any var to screen for debugging * * @params: * $mixed - data of all types * * @return: * html output * **/ public function varDump( $mixed ) { echo '<div><blockquote>'; echo '<pre>'; if ( !$this->vardumpCalled ) { echo '<h4 style="font-family: arial; color:#800080; font-size: 14px; font-weight: bold;">mySQLEz ( v '. mySQLEZ::DB_CLASS_VER .' ) Var Dump...</h4>'; } echo '<p style="font-family: arial; color:#000090; font-size: 12px; font-weight: bold;">'; echo "<strong>Last Query:</strong> " . ( $this->lastQuery ? $this->lastQuery : "NULL" ) . "\n"; echo "<strong>Last Function Call:</strong> " . ( $this->funcCall ? $this->funcCall : "None" ) . "\n"; echo "<strong>Last Rows Returned:</strong> " . count( $this->lastResult ) . "\n"; echo '</font></p></pre></font></blockquote>'; print_r( $mixed ); echo '</div><br /><hr noshade color="ddd" size="1" />'; $this->vardumpCalled = TRUE; } }//class closing tag /* End of file mysqlez.php */ /* Location: /classes/mysqlez.php */