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.

[PHP] MySQL Class

1k views · started by Chris ·
#1
[PHP] MySQL Class
<?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;
}
}
}
?>
#2
Updated script, improved functions.

Also added some new methods, including Query compression.

Benchmarked and is much faster than regular connection methods.
#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.
		//-----------------------------------------------------------
// 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;

}[/0]


i.e. If the query:
SELECT * FROM `users` WHERE 1

was used, the Fetch_Array would return all users in an array.

Either way, solid class.
#4
Artificial wrote:
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.
		//-----------------------------------------------------------
// 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;

}[/0]


i.e. If the query:
SELECT * FROM `users` WHERE 1

was used, the Fetch_Array would return all users in an array.

Either way, solid class.


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 :P

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.