/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class database{ private $conn_id=null; private $_debug=0; private $sql_res=null; private $last_error=null; private $total_queryes=0; public $version=1; public $db_version=null; //private $auth_info=array('host'=>'localhost','user'=>'poimenno','passwd'=>'mdjwg58h','database'=>'poimenno','prefix'=>'futbolka'); private $auth_info=array('host'=>'localhost','user'=>'root','passwd'=>'','database'=>'futbolka','prefix'=>'futbolka'); function __construct(){ if($this->_debug){ } } private function makeFieldsString($data) { $i=0; foreach($data as $k=>$v) { $i++; $fld.=$v; { $fld.=','; } } }else{ $fld="*"; } return $fld; } private function makeLimitString($data) { } private function makeWhereString($data,$type="AND") { $where=null; $i=0; { $where='WHERE '; foreach($data as $k=>$v) { $i++; $where.=$k."='".$v."' "; { $where.=$type." "; } } } return $where; } public function deleteRow($table,$where_clause,$where_type="AND") { { $result=DATABASE_WRONG_PARAM; }else{ $query="DELETE FROM `#prefix#_".$table."`".$this->makeWhereString($where_clause,$where_type); $q=$this->proceedQuery($query); $result=!$this->isError(); } return $result; } /** * * @return * @param $table String New of the source datatable * @param $fields Array Fields list * @param $where Array[optional] Where-clause string * @param $order String[optional] Sorting order * @param $limitation Boolean[optional] Limitation of resulted corteges */ public function getRows($table,$fields,$where=1,$order=false,$limitation=false){ $result=DATABASE_WRONG_PARAM; }else{ $this->makeFieldsString($fields), $table, $this->makeWhereString($where), $ord, $this->makeLimitString($limit) ); #if($table=="clients")die($query); $q=$this->proceedQuery($query); if($this->isError()){ $result=DATABASE_PROCEED_ERROR; }else{ $result=$q; } } return $result; } public function updateRow($table,$updates,$where_clause,$where_type="AND"){ $result=DATABASE_WRONG_PARAM; }else{ $upd=''; $i=0; foreach($updates as $k=>$v){ $i++; $upd.=','; } } $query="UPDATE `#prefix#_".$table."` SET ".$upd." ".$this->makeWhereString($where_clause); $q=$this->proceedQuery($query); $result=!$this->isError(); } return $result; } public function fetchQuery($q){ return $result; } public function setConnection(){ if($this->checkConnection()!=DATABASE_CONNECTION_ESTABILISHED){ if($this->isError()){ $this->sqlErrorExpect(DATABASE_CONNECTION_ERROR,__LINE__,__FILE__); }else{ $this->proceedQuery("SET NAMES utf8"); if($this->isError()) $this->sqlErrorExpect(DATABASE_ACCESS_ERROR,__LINE__,__FILE__); } } } public function getErrorsList($count=2){ $this->temp['_errors']=$this->getProperty('_errors'); for($i=0;$i<$count;$i++){ $this->temp['_result'][]=$this->temp['_errors'][$i]; } }else{ } return $this->temp['_result']; } public function sqlErrorString(){ return $this->getLastError(); } public function getTableFields($table) { $scheme=$this->getRows($table,"*",1); #die(print_r($scheme)); $i=0; { $i++; } } public function insertRow($table,$data){ $query="INSERT into `#prefix#_".$table."` "; $fs=$this->getTableFields($table); $fields=$fs['data']; $count=$fs['count']; $i=0; $fscheme='('; $scheme=''; #die(print_r($data)); foreach($fields as $k=>$v) { $scheme.=($v['type']=='blob' || $v['type']=='string')?'\'':''; $scheme.=$data[$i]; $scheme.=($v['type']=='blob' || $v['type']=='string')?'\'':''; $scheme.=($i<($fs['count']-1))?',':''; $fscheme.='`'.$v['name'].'`'; $fscheme.=($i<($fs['count']-1))?',':''; $i++; } $query.=$fscheme.') VALUES('.$scheme.')'; $q=$this->proceedQuery($query); if($q) { }else{ return false; } } public function checkRowExists($table,$rows,$where_type="AND",$limitation=true){ $this->propertySet('sql_res',null); $table, $this->makeWhereString($rows), ($limitation)?1:''); $q=$this->proceedQuery($query); if(!$this->isError()){ return($this->getNumrows($q)!=0) ; }else{ return DATABASE_PROCEED_ERROR; } return 0; } public function getProperty($property,$class=null){ if(!$class) $result=(in_array($property,get_class_vars(get_class($this))))?$this->$property:DATABASE_PROPERTY_NOT_EXISTS; else $result=(in_array($property,get_class_vars(get_class($class))))?$$class->${$property[0]}[$property[1]]:DATABASE_PROPERTY_NOT_EXISTS; }else{ $class=($class && $class!=null)?$class:'this'; foreach($property as $k=>$v){ if($property_exists){ foreach($property[$k] as $c=>$d){ $result[$d]=$$class->{$k}[$d]; } }else{ $result[$k]=DATABASE_PROPERTY_NOT_EXISTS; } } } return $result; } public function isError(){ } public function closeConnection(){ return (($this->getConnId()!==false)?(@mysql_close($this->getConnId()) && $this->propertySet('conn_id',null)):DATABASE_CONNECTION_NOT_SET); } public function proceedQuery($query){ $this->setConnection(); $this->propertySet("sql_res",null); if($this->isError()) $this->sqlErrorExpect(DATABASE_ACCESS_ERROR,__LINE__,__FILE__); }else{ $this->sqlErrorExpect(DATABASE_INPUT_ERROR,__LINE__,__FILE__); } return $this->getProperty("sql_res"); } public function checkConnection(){ return(($this->getProperty("conn_id")==true)?DATABASE_CONNECTION_ESTABILISHED:DATABASE_CONNECTON_NOT_SET); } public function getNumrows($query_id){ $this->propertySet("sql_res",null); if($query_id){ if($this->isError()) $this->sqlErrorExpect(DATABASE_PROCEED_ERROR,__LINE__,__FILE__); }else{ $this->sqlErrorExpect(DATABASE_INPUT_ERROR,__LINE__,__FILE__); } return $this->getProperty("sql_res"); } public function getSQLParameter($table,$col,$where){ $qStamp=''; $i=0; foreach($col as $k=>$v){ $i++; $qStamp.=$v; if($i>1) $qStamp.=','; } }else{ $qStamp=$col; } $q=$this->proceedQuery(sprintf('SELECT %s FROM `#prefix#_%s` WHERE %s',$this->makeFieldsString($col),$table,$this->makeWhereString($where))); if(!$this->isError()){ if($this->getNumrows($q)!=0){ $row=$this->fetchQuery($q); $this->_result=$row[$col]; }else{ $this->_result=-1; } }else{ $this->_result=-1; } } #die(); return $this->_result; } public function getConnId(){ return $this->getProperty('conn_id'); } public function sqlErrorExpect($msg,$err_line,$err_file){ if($this->_debug)trigger_error($this->getErrorText($msg)."(<pre>Line:".$err_line.";<br/>File:".$err_file."<br/>MySQL Response:".$this->sqlErrorString()."<br/>)</pre>"); } public function getNumcols($query_id){} public function getInstance(){ } public function propertySet($var,$value,$class=null){ if(!$class){ $this->$var=$value; }else{ $class->$var=$value; }else{ return false; } } return true; } } ?>