OOP MySQL class
Dit is een vervanging van de mysqli-class. De performance zal slechter zijn, en ik raad ten alle tijde aan de standaard class te gebruiken als deze aanwezig is. Hij is ook nog in ontwikkeling, want het gedeelte van de prepared statements is nog niet af. De bedoeling is dat deze klasse geinclude kan worden, en dan vrijwel niets veranderd hoeft te worden om hem toch te laten draaien, ook al is het systeem op mysqli gebaseerd. Voor iedereen die zijn steentje wil bijdragen: -Post hier a.u.b. de bugs die je vindt, zodat ik ze kan verhelpen. -Ik ben geen PHP-guru, dus er zullen vast voor bepaalde methoden slimmere oplossingen zijn. Vermeld dit a.u.b. ook, dan leer ik ook weer wat bij. Er zijn nog een aantal verbeteringen nodig: Van de result-class heb ik geen handige oplossing kunnen vinden voor $result->current_field Van de STMT-class heb ik de volgende problemen niet kunnen oplossen: -Data versturen in pakketjes (zowel nodig bij blob als bij send_long_data) -Een vervanging voor bind_result() -Een slimme manier voor $result->lengts -De $result->fetch_field* functies missen nu nog een aantal data-items. Deze worden wel door mysqli maar niet door mysql teruggegeven. Ik heb niet kunnen vinden waar ik deze wel kan krijgen. -Er missen nog een aantal functies in de basisclass, deze zal ik zo snel mogelijk erbij zetten. Al met al: Gebruik deze klasse alleen als er geen mysqli beschikbaar is en je niet de geaveranceerde functies nodig hebt. Ik raad voorlopig het gebruiken van prepared statements ook af, maar je zal er snel achter komen dat deze sowieso nog niet werken volgens mij ;) Veel plezier ermee!
<?PHP
/*
* MySQL class (MySQLi replacement)
* @author SilverWolf
* @version 1.0
* @package MySQL
*
*/
class MySQL{
/**
* Contains the error message if there was a connection problem
* @access public
* @var string
*/
public $connect_error;
/**
* Contains the error code if there was a connection problem
* @access public
* @var int
*/
public $connect_errno;
/**
* Contains the error code if there was an error
* @access public
* @var string
*/
public $error;
/**
* Contains the error code if there was an error
* @access public
* @var int
*/
public $errno=0;
/**
* Contains detailed info about the last query
* @access public
* @var string
*/
public $info;
/**
* Contains the number of affected rows in the last query (same as mysql_num_rows in a SELECT statement)
* @access public
* @var int
*/
public $affected_rows;
/**
* Contains the number of fields in the last query
* @access public
* @var int
*/
public $field_count=0;
/**
* Contains the mysql connection to the database
* @access private
* @var resource of type mysql link
*/
private $connection;
/**
* Contains all the queries of the last multiquery
* @access private
* @var array
*/
private $multi_query_queries=array();
/**
* Contains the offset of the multiquery array, used when fetching next result
* @access private
* @var int
*/
private $mutli_query_offset=0;
/**
* Contains the result of the last query executed
* @access private
* @var MySQL_result
*/
private $multi_query_result;
/*
* The constructor, sets the mysql resource link if data is given
* @param $host string
* @param $user string
* @param $pass string
* @param $db string
*/
public function __construct($host=null,$user=null,$pass=null,$db=null){
if(empty($host)){
return;
}else{
$this->connect($host,$user,$pass,$db);
return;
}
}
/*
* The connector, this actually makes the connection with the database
* @param $host string
* @param $user string
* @param $pass string
* @param $db string
*/
public function connect($host,$user,$pass,$db){
$this->connection=mysql_connect($host,$user,$pass);
if(!$this->connection){
$this->set_error("connection");
$this->connect_error=mysql_error($this->connection);
$this->connect_errno=mysql_errno($this->connection);
return;
}else{
$dbselect=mysql_select_db($db,$this->connection);
if(!$dbselect){
$this->connect_error=mysql_error($this->connection);
$this->connect_errno=mysql_errno($this->connection);
return;
}else{
return;
}
}
}
/*
* Set an error
* @param $type string
*/
public function set_error($type){
if($type=="connection"){
$this->error=mysql_error($this->connection);
$this->errno=mysql_errno($this->connection);
return;
}else{
//Space for other types of errors (STMT)
return;
}
}
/*
* Execute given query
* @param $query string
* @return MySQL_result
* @return boolean
*/
public function query($query){
$this->last_query_result=mysql_query($query,$this->connection);
if($this->last_query_result!==false){
$this->affected_rows=max(mysql_num_rows($this->last_query_result),mysql_affected_rows($this->last_query_result));
$this->info=mysql_info($this->connection);
$insert_id=mysql_insert_id($this->connection);
$this->field_count=@count(mysql_fetch_row($this->last_query_result));
mysql_data_seek($this->last_query_result,0);
return new MySQL_result($this->last_query_result,$insert_id);
}else{
$this->set_error("connection");
return false;
}
}
/*
* Execute given multiquery
* @param $queries string
* @return boolean
*/
public function multi_query($queries){
$this->multi_query_queries=explode(";",$queries);
$this->last_query_result=$this->query($this->multi_query_queries[$this->multi_query_offset]);
$this->multi_query_offset++;
if($this->errno!==0){
return false;
}else{
return true;
}
}
/*
* Store the current result of a multiquery
* @return MySQL_result
*/
public function store_result(){
return $this->last_query_result;
}
/*
* Check if there are more results available in a multiquery
* @return boolean
*/
public function more_results(){
return ($this->multi_query_offset<count($this->multi_query_queries));
}
/*
* Prepare the next result of a multiquery
* @return boolean
*/
public function next_result(){
$this->last_query_result=$this->query($this->multi_query_queries[$this->multi_query_offset]);
$this->multi_query_offset++;
if(($this->errno!==0)&&(!$this->last_query_result)){
return false;
}else{
return true;
}
}
/*
* Prepare the next result of a multiquery
* @param $data mixed
* @return mixed
*/
public function real_escape($data){
if(is_array($data)){
foreach($data as &$value){
$value=mysql_real_escape_string($data,$this->connection);
}
return $data;
}else{
return mysql_real_escape_string($data,$this->connection);
}
}
/*
* Prepare a query
* @param $query string
* @return MySQL_STMT
*/
public function prepare($query){
return new MySQL_STMT($query,$this);
}
/*
* Close the mysql-connection
* @return boolean
*/
public function close(){
return mysql_close($this->connection);
}
/*
* Checks whether the connection to the server is working
* @return boolean
*/
public function ping(){
return mysql_ping($this->connection);
}
/*
* Sets the current active database
* @return boolean
*/
public function select_db($dbname){
return mysql_select_db($dbname,$this->connection);
}
}
/*
* MySQL_result class (MySQLi_result replacement)
* @author SilverWolf
* @version 1.0
* @package MySQL_result
*
*/
class MySQL_result{
/**
* Contains the result given by mysql_query()
* @access protected
* @var resource of type mysql result
*/
protected $result;
/**
* If the result is freed, it cannot be fetched anymore
* @access protected
* @var resource of type mysql result
*/
protected $is_freed=false;
/**
* Contains the number of rows if query was a SELECT query
* @access public
* @var int
*/
public $num_rows;
/**
* Contains the number of fields
* @access public
* @var int
*/
public $field_count;
/*
* The constructor, puts the result, number of rows and id in the appropriate vars
* @param $host string
* @param $user string
* @param $pass string
* @param $db string
*/
public function __construct($result,$id=null){
$this->result=$result;
$this->num_rows=mysql_num_rows($this->result);
$this->field_count=mysql_num_fields($this->result);
return;
}
/*
* Returns an associative array containing the current result row
* @return array
*/
public function fetch_assoc(){
if(!$this->is_freed){
return mysql_fetch_array($this->result,MYSQLI_ASSOC);
}else{
return false;
}
}
/*
* Returns both an asociative and a numeric array containing the current result row
* @return array
*/
public function fetch_array($resulttype=MYSQLI_BOTH){
if(!$this->is_freed){
return mysql_fetch_array($this->result,$resulttype);
}else{
return false;
}
}
/*
* Returns a numeric array containing the current result row
* @return array
*/
public function fetch_row(){
if(!$this->is_freed){
return mysql_fetch_array($this->result,MYSQLI_NUM);
}else{
return false;
}
}
/*
* Returns an object containing the current result row
* @return array
*/
public function fetch_object(){
if(!$this->is_freed){
return mysql_fetch_object($this->result);
}else{
return false;
}
}
/*
* Seek an arbitrary result pointer specified by the offset in the result set
* @return bool
*/
public function data_seek($offset){
return mysql_data_seek($this->result,$offset);
}
/*
* Frees the memory associated with a result, clears the object
* @return bool
*/
public function free(){
mysql_free_result($this->result);
$this->result=null;
$this->is_freed=true;
$this->num_rows=null;
$this->field_count=null;
return;
}
/*
* Sets the field cursor to the given offset
* @return bool
*/
public function field_seek($offset){
return mysql_field_seek($this->result,$offset);
}
/*
* Returns an array of objects which contains field definition information
*
* PLEASE PAY ATTENTION! THIS FUNCTION DOES NOT RETURN THE SAME VALUES AS THE MYSQLI CLASS,
* AND SHOULD BE IMPLEMENTED OTHERWISE! WHEN USING, ALLWAYS CONSULT THE PHP MANUAL ABOUT
* THE MYSQL AND MYSQLI FUNCTIONS ( mysql_fetch_field() and mysqli_fetch_field() )!
*
* @return array
*/
public function fetch_fields(){
$data=array();
$fields=mysql_num_fields($this->result);
for($i=0;$i<$fields;$i++){
$data[]=mysql_fetch_field($this->result,$i);
}
return $data;
}
/*
* Returns the definition of one column of a result set as an object
*
* PLEASE PAY ATTENTION! THIS FUNCTION DOES NOT RETURN THE SAME VALUES AS THE MYSQLI CLASS,
* AND SHOULD BE IMPLEMENTED OTHERWISE! WHEN USING, ALLWAYS CONSULT THE PHP MANUAL ABOUT
* THE MYSQL AND MYSQLI FUNCTIONS ( mysql_fetch_field() and mysqli_fetch_field() )!
*
* @return object
*/
public function fetch_field(){
return mysql_fetch_field($this->result);
}
/*
* Fetch meta-data for a single field
*
* PLEASE PAY ATTENTION! THIS FUNCTION DOES NOT RETURN THE SAME VALUES AS THE MYSQLI CLASS,
* AND SHOULD BE IMPLEMENTED OTHERWISE! WHEN USING, ALLWAYS CONSULT THE PHP MANUAL ABOUT
* THE MYSQL AND MYSQLI FUNCTIONS ( mysql_fetch_field() and mysqli_fetch_field() )!
*
* @return object
*/
public function fetch_field_direct($offset){
if(!$this->field_seek($offset))return false;
return $this->fetch_field();
}
}
/* Define the constants used in the classes
*/
if(!defined("MYSQLI_ASSOC")){
define("MYSQLI_ASSOC",1);
define("MYSQLI_NUM",2);
define("MYSQLI_BOTH",3);
}
/*
* MySQL_STMT class (MySQLi_STMT replacement)
* @author SilverWolf
* @version 1.0
* @package MySQL_STMT
* @info:
This package has not been finished yet. Please don't use prepared statements yet!
*/
class MySQL_STMT{
public $affected_rows;
public $num_rows;
public $errno;
public $error;
public $field_count;
public $param_count=0;
protected $query;
protected $prequery;
protected $original_query;
protected $link;
protected $result;
protected $bound_vars=array();
public function __construct($query,&$link){
$this->link=&$link;
$this->prepare($query);
}
public function prepare($query){
$this->prequery=explode("?",$query);
$this->original_query=$this->prequery;
$this->query=array_shift($this->prequery);
return;
}
public function bind_param(){
$args=func_get_args();
$types=strtolower(array_shift($args));
if(count($args)<1){
$this->link->set_error("other","$mysql_stmt->bind_param() did not get enough arguments!");
}elseif(count($args)==1){
if(is_array($args[0])){
foreach($args[0] as $arg){
$this->add_param($arg,$types);
}
}else{
$this->add_param($args,$types);
}
}else{
$i=0;
foreach($args as $arg){
if(is_array($args)){
foreach($args as $arg){
$this->add_param($arg,$types[$i]);
$i++;
}
}else{
$this->add_param($args,$types[$i]);
$i++;
}
}
}
return;
}
protected function add_param($value,$type){
if($type=="s"){
$this->query.="'".$this->link->real_escape($value)."'".array_shift($this->prequery);
}elseif($type=="i"){
$this->query.=intval($value).array_shift($this->prequery);
}elseif($type=="d"){
$this->query.=doubleval($value).array_shift($this->prequery);
}elseif($type=="b"){
$this->query.="'".$this->link->real_escape($value)."'".array_shift($this->prequery);
}else{
$this->query.="'".$this->link->real_escape($value)."'".array_shift($this->prequery);
}
return;
}
public function bind_result(&$a,&$b=null,&$c=null,&$d=null,&$e=null,&$f=null,&$g=null,&$h=null,&$i=null,&$j=null,&$k=null,&$l=null,&$m=null,&$n=null,&$o=null,&$p=null,&$q=null,&$r=null,&$s=null,&$t=null,&$u=null,&$v=null,&$w=null,&$x=null,&$y=null,&$z=null){
$vars=range('a','z');
foreach($vars as $var){
$this->bound_vars[]=&${$var};
}
}
public function fetch() {
$row = $this->result->fetch_row();
if($row==false){
return false;
}else{
foreach($row as $index => $value){
$this->bound_vars[$index] = $value;
$this->param_count++;
}
return true;
}
}
public function execute(){
$this->result=$this->link->query($this->query);
//asdfasdfasdf
$this->num_rows=$this->result->num_rows;
$this->affected_rows=$this->link->affected_rows;
$this->insert_id=$this->link->insert_id;
$this->field_count=$this->result->field_count;
return;
}
public function data_seek($offset){
return $this->result->data_seek($offset);
}
public function close(){
unset($this);
return;
}
public function store_result(){
return $this->result;
}
}
?>
Reacties
0