PHP Code:
<?php
class MySQL{
private $MySQL_Link = null;
public $MySQL_QueryCount = 0;
public $MySQL_QueryID;
private $MySQL_Host;
private $MySQL_Username;
private $MySQL_Password;
private $MySQL_Database;
/* MySQL Class Construct */
function __construct($Host, $Username, $Password, $Database){
$this->MySQL_Host = $Host;
$this->MySQL_Username = $Username;
$this->MySQL_Password = $Password;
$this->MySQL_Database = $Database;
}
/* MySQL Class Destruction */
function __destruct(){
$this->Close();
unset($this->MySQL_Link);
unset($this->MySQL_QueryCount);
unset($this->MySQL_QueryID);
unset($this->MySQL_Host);
unset($this->MySQL_Username);
unset($this->MySQL_Password);
unset($this->MySQL_Database);
unset($this);
}
/* MySQL Connection */
public function Connect($Username=false, $Password=false, $Database=false){
if($this->MySQL_Link == null && $Username && $Password){
$this->MySQL_Username = $Username;
$this->MySQL_Password = $Password;
if($Database){
$this->MySQL_Database = $Database;
}
@mysql_change_user($this->MySQL_Username, $this->MySQL_Password, $this->MySQL_Database, $this->MySQL_Link);
@end();
}else if($this->MySQL_Link == null && $Username && $Password){
$this->MySQL_Username = $Username;
$this->MySQL_Password = $Password;
if($Database){
$this->MySQL_Database = $Database;
}
}
if($this->MySQL_Link == null){
$this->MySQL_Link = @mysql_connect($this->MySQL_Host, $this->MySQL_Username, $this->MySQL_Password, true, MYSQL_CLIENT_COMPRESS);
if(!$this->MySQL_Link){
$this->MySQL_Link = null;
}else{
if(@mysql_select_db($this->MySQL_Database, $this->MySQL_Link)){
return true;
}
return false;
}
}
if($this->MySQL_Link){
return true;
}else{
die();
}
}
/* MySQL Query */
public function Query($Sql){
$this->MySQL_QueryCount++;
$this->MySQL_QueryID = @mysql_query($Sql, $this->MySQL_Link);
if($this->MySQL_QueryID !== false){
return $this->MySQL_QueryID;
}
return false;
}
/* MySQL Result */
public function Result($QueryID=null, $Row=0, $Field=null){
if($QueryID == null){ $QueryID = $this->MySQL_QueryID; }
if($Field != null){
return @mysql_result($QueryID, $Row, $Field);
}
return @mysql_result($QueryID, $Row);
}
/* MySQL Fetch Array */
public function Fetch_Array($QueryID=null, $ResultType=MYSQL_ASSOC){
if($QueryID == null){ $QueryID = $this->MySQL_QueryID; }
return @mysql_fetch_array($QueryID, $ResultType);
}
/* MySQL Fetch Array */
public function Free_Result($QueryID=null){
if($QueryID == null){ $QueryID = $this->MySQL_QueryID; }
return @mysql_free_result($QueryID);
}
/* MySQL POST/GET Escape */
public function RequestEscape(){
global $_REQUEST;
foreach ($_REQUEST as $Key => $Value) {
$_REQUEST[$Key] = mysql_real_escape_string($Value);
}
}
/* MySQL Escape */
public function Escape($String){
return mysql_real_escape_string($String, $this->MySQL_Link);
}
/* MySQL Close */
public function Close(){
if($this->MySQL_Link != null && $this->MySQL_Link == true){
@mysql_close($this->MySQL_Link);
$this->MySQL_Link = null;
}
}
}
?>
Results 1 to 4 of 4
Thread: [PHP] MySQL Class
- 16 Nov. 2009 05:54am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 500.00
[PHP] MySQL Class
Last edited by Chris; 16 Dec. 2009 at 03:57am.
- 16 Dec. 2009 03:57am #2
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
Updated script, improved functions.
Also added some new methods, including Query compression.
Benchmarked and is much faster than regular connection methods.
- 16 Dec. 2009 09:18pm #3
Just from glancing at it, the only things I might suggest are:
- Return the mysql error in the result of a failed query.
- A function to return multiple rows to an array.
i.e.
PHP Code://-----------------------------------------------------------
// Public function Fetch_Array
// Returns: - Single row if query returns a singular result.
// - Array if query returns multiple rows:
//-----------------------------------------------------------
public function Fetch_Array($QueryID=null, $ResultType=MYSQL_ASSOC){
if($QueryID == null) $QueryID = $this->MySQL_QueryID;
$arr = array();
while($Row = @mysql_fetch_array($QueryID, $ResultType))
$arr[] = $Row;
return count($arr) == 1 ? $arr[0] : $arr;
}
Code:SELECT * FROM `users` WHERE 1
Either way, solid class.
- 17 Dec. 2009 12:16am #4
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00
The function is built similar to the mysql version.
you put the code like so.
while($Row = $MySQL->Fetch_Array()){
echo $Row['id'];
}
That will send back all results
I do not really see a point in sending back an array of all rows, because sometimes not all are used, so loading them as they come seemed to be a better solution. But thanks for the input.
And yeah the error handling in this is currently being worked on, since I never use major error handling I am deciding how I want to implement it.