PHP Code:
<?php
/*
MySQL Class v2
Creation Notes:
Made by Chris Rogers
Made on 09/23/2010
Version 2:
Changed all functions.
Changed variable names.
Changed function names.
Re-organized functions.
Properly commented entire script.
Description:
Creates a simple to use interface between the script and any mysql database.
Functions:
__construct ( $UName = null , $PWord = null , $Addr = null , $Db = null , $Connect = false )
__descruct ( )
open ( $UName = null , $PWord = null , $Addr = null , $Db = null )
close ( )
query ( $sql )
result ( $Query_ID = null , $Row = 0 , $Field = null )
fetch_array ( $Query_ID = null , $Result_Type = MYSQL_ASSOC )
escape ( $String )
free_result ( $Query_ID = null )
count ( )
*/
class MySQL {
protected $Link = null; // Holds connection variable for later calls, prevents unwanted "mysql taps"
protected $UName; // The username to access mysql
protected $PWord; // The password to access mysql
protected $Addr; // The address to access mysql
protected $Db; // The database to access in mysql
protected $Query_ID; // The last query id executed.
protected $Count; // The count of queries executed.
public function __construct ( $UName = null , $PWord = null , $Addr = null , $Db = null , $Connect = false ) {
// Check if the values are set
if ( isset ( $UName ) && isset ( $PWord ) && isset ( $Addr ) && isset ( $Db ) ) {
$this->UName = $UName;
$this->PWord = $PWord;
$this->Addr = $Addr;
$this->Db = $Db;
// Check if we should auto-connect
if ( $Connect === true ) {
// Complete the connection to the database
$this->open ( );
}
}
// Reset count value
$this->Count = 0;
}
public function __destruct ( ) {
// Close any open connection
$this->close ( );
}
public function open ( $UName = null , $PWord = null , $Addr = null , $Db = null ) {
// Check if the values are set
if ( ! empty ( $UName ) && ! empty ( $PWord ) && ! empty ( $Addr ) && ! empty ( $Db ) ) {
$this->UName = $UName;
$this->PWord = $PWord;
$this->Addr = $Addr;
$this->Db = $Db;
}
// Make sure a previous connection does not exist.
if ( $this->Link == null ) {
// Create a new connection
$this->Link = @mysql_connect ( $this->Addr , $this->UName , $this->PWord );
// Verify if the connection was successful
if ( $this->Link ) {
// Connect to the database wanted
if ( @mysql_select_db ( $this->Db , $this->Link ) ) {
// Default value on succesful connection
return true;
}
// Default value on failed connection
return false;
} else {
// Clear current link value to null bcause connection failed
$this->Link = null;
// Default value on failed connection
return false;
}
} else {
// Attempt to close previous connection
$this->close ( );
// Attempt to open the new connection now that the old connection is ended
$this->open ( );
}
// Default value on failed connection
return false;
}
public function close ( ) {
// Check if a valid connection exists
if ( $this->Link != null && $this->Link === true ) {
// Try and close the connection
@mysql_close ( $this->Link );
}
// Clear connection id because the connection is already gone
$this->Link = null;
}
public function query ( $sql ) {
// Add 1 to the query count
$this->Count++;
// Clear previous query results
$this->free_result ( );
// Execute query and save the id
$this->Query_ID = @mysql_query ( $sql , $this->Link );
// Check if the query was successful
if ( $this->Query_ID !== false ) {
// Default value for successful query
return $this->Query_ID;
}
// Default value for failed query
return false;
}
public function result ( $Query_ID = null , $Row = 0 , $Field = null ) {
// Check if the query input is valid
if ( empty ( $Query_ID ) && ! empty ( $this->Query_ID ) ) {
// If not use the one saved in the class
$Query_ID = $this->Query_ID;
}
// Check if the query input is still valid
if ( $Query_ID != null) {
// Check if field is set
if ( ! empty ( $Field ) ) {
// Return the result value
return @mysql_result ( $Query_ID , $Row, $Field );
}
// Return the result value
return @mysql_result ( $Query_ID , $Row );
}
// Default value for invalid query
return false;
}
public function fetch_array ( $Query_ID = null , $Result_Type = MYSQL_ASSOC ) {
// Check if the query input is valid
if ( empty ( $Query_ID ) && ! empty ( $this->Query_ID ) ) {
// If not use the one saved in the class
$Query_ID = $this->Query_ID;
}
// Check if the query input is still valid
if ( $Query_ID != null) {
// Return the fetched array
return @mysql_fetch_array ( $Query_ID , $Result_Type ) ;
}
// Default value for invalid query
return false;
}
public function escape ( $String ) {
// Escape the string in need
return @mysql_real_escape_string ( $String , $this->Link );
}
public function free_result ( $Query_ID = null ) {
// Check if the query input is valid
if ( empty ( $Query_ID ) && ! empty ( $this->Query_ID ) ) {
// If not use the one saved in the class
$Query_ID = $this->Query_ID;
}
// Check if the query input is still valid
if ( $Query_ID != null) {
// Try and free result and return the true/false
return @mysql_free_result ( $QueryID );
}
// Default value for invalid query
return false;
}
public function count ( ) {
return $this->Count;
}
}
?>
Results 1 to 2 of 2
Thread: [Release] MySQL OOPHP Class
- 19 Oct. 2010 07:54pm #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 0.00
[Release] MySQL OOPHP Class
- 01 Nov. 2010 09:43am #2
Should add a few methods to formulate queries so you don't have to rely simply on SQL.
Either way, decent enough class for it's purpose.