A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[Release] MySQL OOP Class [v2]

1.1k views · started by Chris ·
#1
[Release] MySQL OOP Class [v2]
<?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;
}
}
?>[/0][/0][/0][/0][/0][/0][/1][/0][/0][/1][/1][/0][/1][/0][/1][/0][/0][/0][/0]


This version comes packed with quite a wide range of capabilities.

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;

connect ( $host = null , $username = null , $password = null , $db = null )

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.

close ( )

Closes the mysql connection.

query ( $sql )

Executes a prepared statement.

insert ( $table , $input = array ( ) )

Prepares then executes the prepared insert statement. Statement is prepared using a key/value system. The inputted array should look like so;
array ( 'key' => 'value', 'key' => 'value' )

Automatically escapes all input.

update ( $table , $input , $where = "" )

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;
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.

fetch_array ( $mysql_id = null , $resultype = MYSQL_ASSOC )

Will return an array of all rows affected.

result ( $mysql_id = null , $row = 0 , $field = null )

Will return the result desired.

free_result ( $mysql_id = null )

Free's the previous queries result ( Executed automatically every time a query is executed. )

escape ( $string )

Sanitizes both strings and array's.

global_escape ( )

Sanitizes $_GET, $_POST, and $_COOKIE

error ( )

Returns an array of the last function and query executed.

isMySQL ( )

Returns true, used for checking if this class is initiated from outside classes.


I hope this class helps you!
#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.
#3
HTML wrote:
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.


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.
#4
Alright, I'll get started ASAP. I also like : Upload/Download Management

Thanks bby.
#5
HTML wrote:
Alright, I'll get started ASAP. I also like : Upload/Download Management

Thanks bby.


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
#6
It looks good.

You should look in to PDO. Definitely the next step from here.