This version comes packed with quite a wide range of capabilities.PHP Code:
<?php
class MySQL
{
private $mysql;
private $query_id;
private $host;
private $username;
private $password;
private $db;
private $error;
private $globaled = false;
private $connected = false;
public function __construct ( $host = null , $username = null , $password = null , $db = null )
{
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
$this->error = array ( );
$this->error[] = array ( 'Function' , '__contruct' );
$this->error[] = array ( 'Query' , '' );
}
public function connect ( $host = null , $username = null , $password = null , $db = null )
{
$this->error[0] = array ( 'Function' , 'connect' );
$this->host = ( ! empty ( $host ) && $host != $this->host ) ? $host : $this->host;
$this->username = ( ! empty ( $username ) && $username != $this->username ) ? $username : $this->username;
$this->password = ( ! empty ( $password ) && $password != $this->password ) ? $password : $this->password;
$this->db = ( ! empty ( $db ) && $db != $this->db ) ? $db : $this->db;
if ( ( $this->mysql = mysql_connect ( $this->host , $this->username , $this->password ) ) == true )
{
if ( mysql_select_db ( $this->db , $this->mysql ) == true )
{
$this->connected = true;
}
}
}
public function close ( )
{
$this->error[0] = array ( 'Function' , 'close' );
if ( $this->connected = true )
{
$this->free_result ( );
@mysql_close ( $this->mysql );
$this->mysql = null;
$this->query_id = null;
$this->connected = false;
}
}
public function query ( $sql )
{
$this->error[0] = array ( 'Function' , ( ( $this->error[0][1] == 'insert' || $this->error[0][1] == 'update' ) ? $this->error[0][1] : 'query' ) );
$this->error[1] = array ( 'Query' , $sql );
$this->free_result ( );
if ( $this->connected = true )
{
if ( ( $this->query_id = @mysql_query ( $sql , $this->mysql ) ) == true )
{
return $this->query_id;
}
}
return false;
}
public function insert ( $table , $input = array ( ) )
{
$this->error[0] = array ( 'Function' , 'insert' );
if ( is_array ( $input ) )
{
$keys = "";
$values = "";
foreach ( $input as $key => $value )
{
$keys .= "`" . $this->escape ( $key ) . "` , ";
$values .= "'" . $this->escape ( $value ) . "' , ";
}
$keys = rtrim ( $keys , ' , ' );
$values = rtrim ( $values , ' , ' );
$sql = "INSERT INTO `" . $this->escape ( $table ) . "` (" . $keys . ") VALUES (" . $values . ");";
return $this->query ( $sql );
}
return false;
}
public function update ( $table , $input , $where = "" )
{
$this->error[0] = array ( 'Function' , 'update' );
$sqlwhere = $where;
if ( is_array ( $where ) )
{
$sqlwhere = "";
foreach ( $where as $key => $value )
{
$type = 'AND';
if ( is_array ( $value ) )
{
if ( $value[1] == 1 )
{
$type = 'OR';
}
$value = $value[0];
}
$sqlwhere .= "`" . $this->escape ( $key ) . "` = '" . $this->escape ( $value ) . "' " . $type . " ";
}
$sqlwhere = rtrim ( rtrim ( $sqlwhere , ' OR ' ) , ' AND ' );
}
if ( is_array ( $input ) )
{
$values = "";
foreach ( $input as $key => $value )
{
$values .= "`" . $this->escape ( $key ) . "` = '" . $this->escape ( $value ) . "' , ";
}
$values = rtrim ( $values , ' , ' );
$sql = "UPDATE `" . $this->escape ( $table ) . "` SET " . $values . ( $sqlwhere != "" ? ( ' WHERE ' . $sqlwhere ) : '' ) . ";";
}
else
{
$sql = "UPDATE `" . $this->escape ( $table ) . "` SET " . $values . ( $sqlwhere != "" ? ( ' WHERE ' . $sqlwhere ) : '' ) . ";";
}
return $this->query ( $sql );
}
public function fetch_array ( $mysql_id = null , $resultype = MYSQL_ASSOC ){
$this->error[0] = array ( 'Function' , 'fetch_array' );
if ( $this->connected = true )
{
$mysql_id = ( $mysql_id == null ) ? $this->query_id : $mysql_id;
return @mysql_fetch_array ( $mysql_id, $resultype );
}
return null;
}
public function result ( $mysql_id = null , $row = 0 , $field = null )
{
$this->error[0] = array ( 'Function' , 'result' );
if ( $this->connected = true )
{
$mysql_id = ( $mysql_id == null ) ? $this->query_id : $mysql_id;
if ( $field != null )
{
return @mysql_result ( $mysql_id , $row , $field );
}
return @mysql_result ( $mysql_id , $row );
}
return false;
}
public function free_result ( $mysql_id = null )
{
$this->error[0] = array ( 'Function' , 'free_result' );
if ( $this->connected = true )
{
$mysql_id = ( $mysql_id == null ) ? $this->query_id : $mysql_id;
return @mysql_free_result( $mysql_id );
}
return false;
}
public function escape ( $string )
{
$this->error[0] = array ( 'Function' , 'escape' );
if ( $this->connected === true )
{
if ( is_array ( $string ) )
{
$estring = array ( );
foreach ( $string as $key => $value )
{
$estring[$key] = mysql_real_escape_string ( $value , $this->mysql );
}
return $estring;
}
return mysql_real_escape_string ( $string , $this->mysql );
}
if ( is_array ( $string ) )
{
$estring = array ( );
foreach ( $string as $key => $value )
{
$estring[$key] = add_slashes ( $value , $this->mysql );
}
return $estring;
}
return add_slashes ( $string );
}
public function global_escape ( )
{
$this->error[0] = array ( 'Function' , 'global_escape' );
if ( $this->globaled === false )
{
$globe = array ( $_GET , $_POST , $_COOKIE );
for ( $i = 0; $i < sizeOf ( $globe ); $i++ )
{
$globe[$i] = escape ( $globe[$i] );
}
}
$this->globaled = true;
}
public function error ( )
{
return $this->error;
}
public function isMySQL ( )
{
return true;
}
}
?>
In it is a full tracking system, allowing you to look at the last function/query used at any point, helpful for debugging/error handling.
It comes with 12 functions;
Handles connection to the mysql database, all vlaues can be left empty [ connect ( ) ] and the script will revert to using the information entered on construction.PHP Code:
connect ( $host = null , $username = null , $password = null , $db = null )
Closes the mysql connection.PHP Code:
close ( )
Executes a prepared statement.PHP Code:
query ( $sql )
Prepares then executes the prepared insert statement. Statement is prepared using a key/value system. The inputted array should look like so;PHP Code:
insert ( $table , $input = array ( ) )
array ( 'key' => 'value', 'key' => 'value' )
Automatically escapes all input.
Prepares then executes the prepared update statement. Statement is prepared using a key/value system ( or raw input [ "`key` = 'value'" ] ). The inputted array should look like so;PHP Code:
update ( $table , $input , $where = "" )
array ( 'key' => 'value', 'key' => 'value' )
The where value can be raw ( "`key` = 'value'" ) or in an array form like the one mentioned above. However you can identify the type ( AND, OR ) of the where values like so;
array ( 'key' => 'value', 'key' => array ( 'value' , 1 ) )
That array means the second value will be an OR, (0/null = AND 1 = OR)
Automatically escapes all input.
Will return an array of all rows affected.PHP Code:
fetch_array ( $mysql_id = null , $resultype = MYSQL_ASSOC )
Will return the result desired.PHP Code:
result ( $mysql_id = null , $row = 0 , $field = null )
Free's the previous queries result ( Executed automatically every time a query is executed. )PHP Code:
free_result ( $mysql_id = null )
Sanitizes both strings and array's.PHP Code:
escape ( $string )
Sanitizes $_GET, $_POST, and $_COOKIEPHP Code:
global_escape ( )
Returns an array of the last function and query executed.PHP Code:
error ( )
Returns true, used for checking if this class is initiated from outside classes.PHP Code:
isMySQL ( )
I hope this class helps you!
Results 1 to 6 of 6
Thread: [Release] MySQL OOP Class [v2]
- 24 Dec. 2010 04:14am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 10.00
[Release] MySQL OOP Class [v2]
- 24 Dec. 2010 04:36am #2
Revamped the hell out of it in 2.0 huh? Very nice class ._______. I'm jelly. Do you have any suggestions on what other classes I could release? I have a simple register class, I need ideas.
- 24 Dec. 2010 05:08am #3
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 10.00
Thank you I appreciate the support.
Hmm...well some classes I think are nifty are;
Benchmarking
Cache
Upload/Download Management
Resource Management
Configuration ( Settings List, running off MySQL, XML, JSON, and Text )
I think Benchmarking and Configuration would be a nice thing for you, they really make you work since they both use a lot of time and coding.
Also for your registration allow the user to customize the MySQL data.
For example allow the user to set the "username" field to lets say "bullshit" so their mysql datbase can be customized, without needing to hardcode the new fields name. This is known as soft-coding, it is EXTREMELY neccesary for things like register/login.
Say I wanted to change the field "username" to "name" in my db, why need to spend 20 minutes changing every "username" to "name" in the php code, when I can soft-code in a variable to handle it.
- 24 Dec. 2010 05:22am #4
Alright, I'll get started ASAP. I also like : Upload/Download Management
Thanks bby.
- 24 Dec. 2010 07:20am #5
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 11.85
If you do make upload/download management, you might have problems with limiting the rates ( kb/s etc. ) if you do get stuck talk to me over msn, my address is simplifiedlogic@yahoo.com
- 27 Dec. 2010 09:29am #6
It looks good.
You should look in to PDO. Definitely the next step from here.