Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Baser_i {
- const LOGFILE='../baserilog.txt';
- static private $instance=NULL;
- private $mysqli;
- private $mysqli_stmt;
- private $mysqli_result;
- static private $myhost='';
- static private $mylogin='';
- static private $mypass='';
- static private $myname='';
- protected $connected=false;
- public $test_stmt=true;
- public $logfile;
- //----------------------------------------
- static public function init($inhost,$inlogin,$inpass,$inname) {
- self::$myhost=$inhost;
- self::$mylogin=$inlogin;
- self::$mypass=$inpass;
- self::$myname=$inname;
- }
- //----------------------------------------
- static public function getInstance() {
- if (self::$instance==null) {
- self::$instance= new Baser_i();
- }
- return self::$instance;
- }
- //---------------------------------------??
- static public function freeInstance() {
- if (isset(self::$instance))
- self::$instance=NULL;
- }
- //----------------------------------------
- private function __construct() {
- $this->init_logfile();
- $this->connect();
- }
- //----------------------------------------
- public function init_logfile() {
- if (isset($_SESSION["logfile"]))
- $this->logfile=$_SESSION["logfile"];
- else
- {
- $zdir=substr(Baser_i::LOGFILE, 0, strrpos(Baser_i::LOGFILE,'/')+1);
- $zfile=substr(Baser_i::LOGFILE,strrpos(Baser_i::LOGFILE,'/')+1);
- $zfile=str_replace('.','_'.date('d_m_Y_H_I_s').'.',$zfile);
- $this->logfile=$zdir.$zfile;
- $_SESSION["logfile"]=$this->logfile;
- }
- }
- //----------------------------------------
- public function logt($instr) {
- if (file_exists($this->logfile))
- $fh=fopen($this->logfile,'ab');
- else
- $fh=fopen($this->logfile,'wb');
- fwrite($fh, date('d.m.Y H:i:s').' '.$instr."\r\n");
- fclose($fh);
- }
- //----------------------------------------
- public function __destruct() {
- if ($this->connected) {
- $this->disconnect();
- }
- }
- //----------------------------------------
- public function connect() {
- $this->mysqli= new mysqli(self::$myhost, self::$mylogin,
- self::$mypass, self::$myname);
- if ($this->mysqli->connect_errno) {
- $this->logt($this->connect_error);
- }
- else {
- $this->connected=true;
- //if (!$this->mysqli->set_charset('cp1251'))
- //echo 'codepage set error '.$this->mysqli->error;
- $this->mysqli->query("SET NAMES 'cp1251'");
- $infos=array('character_set_name',
- 'client_info',
- 'client_version',
- 'protocol_version',
- 'server_info',
- 'server_version');
- $this->logt("");
- foreach ($infos as $info) {
- if (method_exists($this->mysqli,$info))
- $this->logt($info.": ". call_user_func(array($this->mysqli,$info)));
- else
- $this->logt($info.": ". $this->mysqli->$info);
- }
- }
- }
- //----------------------------------------
- public function disconnect() {
- if (!$this->connected) return;
- $this->mysqli->close();
- }
- //----------------------------------------
- private function checksqlerror($agent) {
- if ($agent->errno!=0) {
- $this->logt($agent->error);
- }
- return ($agent->errno==0);
- }
- //----------------------------------------
- public function insertQuery($insql,$fields, $values) {
- if (!$this->connected) return;
- $qtype= (strstr($insql,'?')!=='');
- switch ($qtype) {
- //---------------
- case false:
- $this->logt($insql);
- $insql=str_replace('%fields%',implode(',',$fields),$insql);
- $insql=str_replace('%values%',implode(',',$values),$insql);
- $this->logt($insql);
- $this->mysqli->query($insql);
- if ($this->checksqlerror($this->mysqli)) {
- $this->logt("affected rows: {$this->mysqli->affected_rows}");
- $this->logt("last insert id: {$this->mysqli->insert_id}");
- return $this->mysqli->insert_id;
- }
- else {
- return false;
- }
- //--------------
- case true:
- $this->logt($insql);
- $this->mysqli_stmt=$this->mysqli->prepare($insql);
- if (!$this->mysqli_stmt) {
- $this->logt("error preparing $insql");
- return false;
- }
- $intype='';
- $invars=array();
- for ($i=0; $i<sizeof($values); $i++) {
- $this->logt("type of {$values[$i]} is ".gettype($values[$i]));
- if (is_scalar($values[$i])) {
- if (is_int($values[$i]) )
- $intype.='i';
- else
- if (is_float($values[$i])) {
- $intype.='d';
- }
- else if (is_string($values[$i])) {
- $intype.='s';
- }
- else
- $intype.='s';
- $invars[]=& $values[$i];
- }
- else {
- $intype.='s';
- $invars[]= & $values[$i];
- $values[$i]= serialize($values[$i]);
- }
- }
- $this->logt("intype=$intype");
- $this->logt('invars='.print_r($invars));
- array_unshift($invars,$intype);
- call_user_func_array(array($this->mysqli_stmt,"bind_param"), $invars);
- $this->mysqli_stmt->execute();
- if ($this->checksqlerror($this->mysqli_stmt)) {
- $this->logt("affectted_rows: {$this->mysqli_stmt->affected_rows}");
- $this->logt("last insert id: {$this->mysqli_stmt->insert_id}");
- return $this->mysqli_stmt->insert_id;
- }
- else {
- return false;
- }
- }
- //----------------------------
- }
- //----------------------------------------
- public function execQuery($insql, $a) {
- if (!$this->connected) return;
- $n= substr_count($insql, '?');
- switch ($n==0) {
- //-----------------------------
- case true:
- $this->mysqli->query($insql);
- if ($this->checksqlerror($this->mysqli)) {
- $this->logt("affected rows: {$this->mysqli->affected_rows}");
- return $this->mysqli->affected_rows;
- } else {
- return false;
- }
- //-----------------------------
- case false:
- $this->mysqli_stmt=$this->mysqli->stmt_init();
- if (!$this->mysqli_stmt->prepare($insql)) {
- $this->logt('error preparing: '.$insql);
- return false;
- };
- switch ($n) {
- case 1:
- $this->mysqli_stmt->bind_param('s',$a[0]);
- break;
- case 2:
- $this->mysqli_stmt->bind_param('ss',$a[0],$a[1]);
- break;
- case 3:
- $this->mysqli_stmt->bind_param('sss',$a[0],$a[1],$a[2]);
- break;
- case 4:
- $this->mysqli_stmt->bind_param('ssss',$a[0],$a[1],$a[2],$a[3]);
- }
- $this->logt("execute: $insql");
- $this->mysqli_stmt->execute();
- if ($this->checksqlerror($this->mysqli_stmt)) {
- $afrows=$this->mysqli_stmt->affected_rows;
- $this->logt("affected rows: $afrows");
- }
- $this->mysqli_stmt->close();
- return (isset($afrows)) ? $afrows : false;
- }
- }
- //----------------------------------------
- public function getSingle($insql) {
- }
- //----------------------------------------
- public function getQuery($insql, $start=0, $num=50000, $restype='std') {
- if (!$this->connected) return false;
- if ($result=$this->mysqli->query($insql)) {
- $outz=array();
- $result->data_seek($start-1);
- switch ($restype) {
- case 'std': $arr_type=MYSQLI_NUM; break;
- case 'inf': $arr_type=MYSQLI_ASSOC;break;
- default: $arr_type=MYSQLI_BOTH;break;
- }
- $i=0;
- while (($row=$result->fetch_array($arr_type)) && ($i<$num)) {
- $outz[]=$row;
- $i++;
- }
- $result->close();
- return($outz);
- } else {
- return false;
- }
- }
- //----------------------------------------
- public function getQueryAll($insql, $param=1) {
- if (!$this->connected) return false;
- $this->mysqli_stmt=$this->mysqli->stmt_init();
- if (!$this->mysqli_stmt->prepare($insql)) {
- $this->logt("Error preparing $insql");
- return false;
- }
- $this->mysqli_stmt->bind_param("i",$param_var);
- $tmparr=array();
- if (is_array($param)) {
- foreach ($param as $param_var) {
- $this->mysqli_stmt->execute();
- $this->mysqli_stmt->bind_result($col1, $col2);
- while ($this->mysqli_stmt->fetch()) {
- $tmparr[]=array($col1, $col2);
- }
- //$result=$this->mysqli_stmt->get_result();
- /*$tmparr=array_merge($tmparr,
- $result->fetch_all(MYSQLI_BOTH));*/
- }
- }
- else {
- $param_var=$param;
- $this->mysqli_stmt->execute();
- $this->mysqli_stmt->bind_result($col1,$col2);
- while ($this->mysqli_stmt->fetch()) {
- $tmparr[]=array($col1,$col2);
- }
- }
- $this->mysqli_stmt->close();
- return $tmparr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement