lunes, 9 de noviembre de 2009

MYSQL Class


<font class="Apple-style-span" face="'Courier New', Courier, monospace">// for :MySQL class //************************************** Updates & more info: http://code.bn2vs.com/viewtopic.php?t=129 //************************************** // Name: MySQL class // Description:A very easy-to-use mysql database class. This class enables you to interact with a mysql database in a very fluent, oop and logical way. Using it will reduce the amount of errors in your code, make it shorter and easier to read, make it shorter, and optimize memory usage. // By: [RTS]BN+VS* // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.2614/lngWId.8/qx/vb/scripts/ShowCode.htm //for details. //************************************** <?php /** * BN+ PHP Library * * bn.mysql.php * Version 1.1.4 * * By De Dauw Jeroen - jeroendedauw@gmail.com * January 2009 * * Class based upon work by Dimitar Angelov, February 2008 */ class MySQL { public $sqlServer = 'localhost'; public $sqlUser = 'root'; public $sqlPassword = ''; public $database; public $last_query = ''; private $connection; private $query_result; public function __construct() {} public function __destruct() { $this->Close(); } //+======================================================+ // Create a connection to the MySQL database //+======================================================+ public function Connect($server = null, $user = null, $password = null, $database = null){ if (isset($server)) $this->sqlServer = $server; if (isset($user)) $this->sqlUser = $user; if (isset($password)) $this->sqlPassword = $password; if (isset($database)) $this->database = $database; $this->connection = mysql_connect($this->sqlServer, $this->sqlUser, $this->sqlPassword); if($this->connection){ if (mysql_select_db($this->database)){ return $this->connection; }else{ return $this->error(); } }else{ return $this->error(); } } //+======================================================+ // Execute a query //+======================================================+ public function Query($query, $die = false){ if ($query != NULL){ $this->last_query = $query; $this->query_result = mysql_query($query, $this->connection); if(!$this->query_result){ if ($die) die("die: ".$this->query_result); return $this->error(); }else{ if ($die) die("die: ".$this->query_result); return $this->query_result; } }else{ echo "Empty query cannot be executed!"; } } //+======================================================+ // Returns the result //+======================================================+ public function GetResult(){ return $this->query_result; } //+======================================================+ // Returns the connection //+======================================================+ public function GetConnection(){ return $this->connection; } //+======================================================+ // Returns an object with properties representing the result fields and values //+======================================================+ public function GetObject($qry = null){ if (isset($qry)) $this->query($qry); return mysql_fetch_object($this->GetResult()); } //+======================================================+ // Returns an array with keys representing the result fields and values //+======================================================+ public function GetArray($query_id = ""){ if($query_id == NULL){ $return = mysql_fetch_array($this->GetResult()); }else{ $return = mysql_fetch_array($query_id); } return $return ? $return : $this->error(); } //+======================================================+ // Returns the number of rows in the result //+======================================================+ public function GetNumRows($qry = null){ if (isset($qry)) $this->Query($qry); $amount = mysql_num_rows($this->GetResult()); return empty($amount) ? 0 : $amount; } //+======================================================+ // Returns if the result contains rows //+======================================================+ public function HasResults($qry = null) { if (isset($qry)) $this->Query($qry); return $this->GetNumRows($qry) > 0; } //+======================================================+ // Returns the number of rows that where affected by the last action //+======================================================+ public function GetAffectedRows($qry = null, $query_id = null){ if (isset($qry)) $this->Query($qry); if(empty($query_id)){ $return = mysql_affected_rows($this->GetResult()); }else{ $return = mysql_affected_rows($query_id); } return $return ? $return : $this->error(); } //+======================================================+ // Returns the auto generated id from the last insert action //+======================================================+ public function GetInsertId($connection_link = null){ return mysql_insert_id(isset($connection_link) ? $connection_link : $this->connection); } //+======================================================+ // Close the connection to the MySQL database //+======================================================+ public function Close(){ if(isset($this->connection)){ return @mysql_close($this->connection); } else { return $this->error(); } } //+======================================================+ // Outputs the MySql error //+======================================================+ private function error(){ if(mysql_error() != ''){ echo '<b>MySQL Error</b>: '.mysql_error().'<br/>'; } } } ?></font>

No hay comentarios.: