Advertisement
terorama

csv / baser_i.inc.php

Aug 28th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.67 KB | None | 0 0
  1. <?php
  2.  
  3. class Baser_i {
  4.  
  5.         const LOGFILE='../baserilog.txt';
  6.        
  7.         static private $instance=NULL;
  8.        
  9.         private $mysqli;
  10.         private $mysqli_stmt;
  11.         private $mysqli_result;
  12.        
  13.         static private $myhost='';
  14.         static private $mylogin='';
  15.         static private $mypass='';
  16.         static private $myname='';
  17.        
  18.         protected $connected=false;
  19.        
  20.         public $test_stmt=true;
  21.         public $logfile;
  22.        
  23.         //----------------------------------------
  24.         static public function init($inhost,$inlogin,$inpass,$inname) {
  25.        
  26.            self::$myhost=$inhost;
  27.            self::$mylogin=$inlogin;
  28.            self::$mypass=$inpass;
  29.            self::$myname=$inname;
  30.         }
  31.         //----------------------------------------
  32.         static public function getInstance() {
  33.        
  34.            if (self::$instance==null) {
  35.               self::$instance= new Baser_i();
  36.            }
  37.            
  38.            return self::$instance;
  39.         }
  40.         //---------------------------------------??
  41.         static public function freeInstance() {
  42.            
  43.            if (isset(self::$instance))
  44.               self::$instance=NULL;
  45.         }
  46.        
  47.         //----------------------------------------
  48.         private function __construct() {
  49.        
  50.            $this->init_logfile();
  51.            
  52.            $this->connect();
  53.         }
  54.        
  55.         //---------------------------------------- 
  56.         public function init_logfile() {
  57.    
  58.            if (isset($_SESSION["logfile"]))
  59.               $this->logfile=$_SESSION["logfile"];
  60.            else
  61.               {
  62.               $zdir=substr(Baser_i::LOGFILE, 0, strrpos(Baser_i::LOGFILE,'/')+1);
  63.               $zfile=substr(Baser_i::LOGFILE,strrpos(Baser_i::LOGFILE,'/')+1);
  64.              
  65.               $zfile=str_replace('.','_'.date('d_m_Y_H_I_s').'.',$zfile);
  66.              
  67.               $this->logfile=$zdir.$zfile;
  68.              
  69.              
  70.               $_SESSION["logfile"]=$this->logfile;
  71.               }
  72.              
  73.         }
  74.        
  75.         //----------------------------------------
  76.         public function logt($instr) {
  77.            
  78.            if (file_exists($this->logfile))
  79.               $fh=fopen($this->logfile,'ab');
  80.            else
  81.               $fh=fopen($this->logfile,'wb');
  82.            
  83.            fwrite($fh, date('d.m.Y H:i:s').' '.$instr."\r\n");         
  84.            fclose($fh);
  85.         }
  86.        
  87.         //----------------------------------------         
  88.        
  89.         public function __destruct() {
  90.            if ($this->connected) {
  91.               $this->disconnect();
  92.            }
  93.            
  94.         }
  95.         //----------------------------------------     
  96.         public function connect() {
  97.        
  98.            $this->mysqli= new mysqli(self::$myhost, self::$mylogin,  
  99.                         self::$mypass, self::$myname);
  100.                        
  101.             if ($this->mysqli->connect_errno) {
  102.            
  103.                $this->logt($this->connect_error);
  104.                
  105.                }
  106.             else {
  107.                $this->connected=true;
  108.                
  109.                //if (!$this->mysqli->set_charset('cp1251'))
  110.                //echo 'codepage set error '.$this->mysqli->error;
  111.                
  112.                $this->mysqli->query("SET NAMES 'cp1251'");
  113.                  
  114.                $infos=array('character_set_name',
  115.                             'client_info',
  116.                             'client_version',
  117.                             'protocol_version',
  118.                             'server_info',
  119.                             'server_version');
  120.                            
  121.                $this->logt("");        
  122.                foreach ($infos as $info) {
  123.                
  124.                   if (method_exists($this->mysqli,$info))
  125.                      $this->logt($info.": ". call_user_func(array($this->mysqli,$info)));            
  126.                   else
  127.                      $this->logt($info.": ". $this->mysqli->$info);
  128.                
  129.            
  130.                }
  131.  
  132.             }
  133.            
  134.            
  135.         }
  136.      
  137.         //----------------------------------------         
  138.         public function disconnect() {
  139.        
  140.            if (!$this->connected) return;
  141.            $this->mysqli->close();
  142.         }
  143.         //----------------------------------------
  144.         private function checksqlerror($agent) {
  145.        
  146.            if ($agent->errno!=0) {
  147.  
  148.               $this->logt($agent->error);
  149.            }
  150.            
  151.            return ($agent->errno==0);
  152.         }
  153.         //---------------------------------------- 
  154.        
  155.         public function insertQuery($insql,$fields, $values) {
  156.            
  157.            if (!$this->connected) return;
  158.            
  159.            $qtype= (strstr($insql,'?')!=='');
  160.            
  161.            
  162.            switch ($qtype) {
  163.            
  164.            //---------------
  165.            case false:
  166.            
  167.               $this->logt($insql);
  168.               $insql=str_replace('%fields%',implode(',',$fields),$insql);
  169.               $insql=str_replace('%values%',implode(',',$values),$insql);  
  170.               $this->logt($insql);
  171.              
  172.               $this->mysqli->query($insql);
  173.            
  174.               if ($this->checksqlerror($this->mysqli)) {
  175.  
  176.                  $this->logt("affected rows: {$this->mysqli->affected_rows}");
  177.                  $this->logt("last insert id: {$this->mysqli->insert_id}");
  178.                  
  179.                  return $this->mysqli->insert_id;
  180.               }
  181.               else {
  182.                  
  183.              
  184.                  return false;
  185.                  }
  186.                  
  187.             //--------------
  188.             case true:
  189.                $this->logt($insql);
  190.                $this->mysqli_stmt=$this->mysqli->prepare($insql);
  191.                
  192.                if (!$this->mysqli_stmt) {
  193.                   $this->logt("error preparing $insql");
  194.                   return false;
  195.                }
  196.                
  197.                $intype='';
  198.                $invars=array();
  199.                for ($i=0; $i<sizeof($values); $i++) {
  200.                  
  201.                   $this->logt("type of {$values[$i]} is ".gettype($values[$i]));
  202.                
  203.                   if (is_scalar($values[$i])) {
  204.                  
  205.                      if (is_int($values[$i])  )
  206.                         $intype.='i';
  207.                      else
  208.                         if (is_float($values[$i])) {
  209.                            $intype.='d';
  210.                         }
  211.                         else if (is_string($values[$i])) {
  212.                               $intype.='s';
  213.                            }
  214.                         else
  215.                            $intype.='s';
  216.                        
  217.                            
  218.                      $invars[]=& $values[$i];
  219.                      
  220.                   }
  221.                   else {
  222.                      $intype.='s';
  223.                      $invars[]= & $values[$i];
  224.                      $values[$i]= serialize($values[$i]);
  225.                   }                                                
  226.                }
  227.                
  228.                
  229.                $this->logt("intype=$intype");
  230.                $this->logt('invars='.print_r($invars));
  231.                array_unshift($invars,$intype);
  232.                
  233.                
  234.                call_user_func_array(array($this->mysqli_stmt,"bind_param"), $invars);
  235.                
  236.                $this->mysqli_stmt->execute();
  237.                
  238.                if ($this->checksqlerror($this->mysqli_stmt)) { 
  239.                  
  240.                  $this->logt("affectted_rows: {$this->mysqli_stmt->affected_rows}");
  241.                  $this->logt("last insert id: {$this->mysqli_stmt->insert_id}");
  242.                  return $this->mysqli_stmt->insert_id;
  243.                }
  244.                else {
  245.                   return false;
  246.                }
  247.                
  248.  
  249.             }
  250.             //----------------------------
  251.            
  252.         }
  253.         //----------------------------------------     
  254.         public function execQuery($insql, $a) {
  255.        
  256.            if (!$this->connected) return;
  257.            
  258.            $n= substr_count($insql, '?');
  259.            switch ($n==0) {
  260.               //-----------------------------
  261.               case true:
  262.                  $this->mysqli->query($insql);
  263.                  
  264.                  if ($this->checksqlerror($this->mysqli)) {
  265.                     $this->logt("affected rows: {$this->mysqli->affected_rows}");
  266.                    
  267.                     return $this->mysqli->affected_rows;
  268.                    
  269.                  } else {
  270.                     return false;
  271.                  }
  272.                  
  273.              
  274.              
  275.               //-----------------------------
  276.               case false:
  277.                  $this->mysqli_stmt=$this->mysqli->stmt_init();
  278.            
  279.                  if (!$this->mysqli_stmt->prepare($insql)) {
  280.                     $this->logt('error preparing: '.$insql);
  281.                     return false;
  282.                  };
  283.                  
  284.                  switch ($n) {
  285.                     case 1:
  286.                        $this->mysqli_stmt->bind_param('s',$a[0]);
  287.                        break;
  288.                     case 2:
  289.                        $this->mysqli_stmt->bind_param('ss',$a[0],$a[1]);
  290.                        break;
  291.                     case 3:
  292.                        $this->mysqli_stmt->bind_param('sss',$a[0],$a[1],$a[2]);
  293.                        break;
  294.                     case 4:
  295.                        $this->mysqli_stmt->bind_param('ssss',$a[0],$a[1],$a[2],$a[3]);
  296.                  }
  297.            
  298.               $this->logt("execute: $insql");
  299.               $this->mysqli_stmt->execute();
  300.            
  301.               if ($this->checksqlerror($this->mysqli_stmt)) {
  302.              
  303.                  $afrows=$this->mysqli_stmt->affected_rows;
  304.                  $this->logt("affected rows: $afrows");
  305.  
  306.               }
  307.               $this->mysqli_stmt->close();
  308.              
  309.               return (isset($afrows)) ? $afrows : false;
  310.              
  311.            }    
  312.         }
  313.         //----------------------------------------     
  314.         public function getSingle($insql) {
  315.            
  316.         }
  317.         //----------------------------------------     
  318.         public function getQuery($insql, $start=0, $num=50000, $restype='std') {
  319.        
  320.            if (!$this->connected) return false;
  321.            
  322.            if ($result=$this->mysqli->query($insql)) {
  323.            
  324.               $outz=array();
  325.              
  326.               $result->data_seek($start-1);
  327.              
  328.              
  329.               switch ($restype) {
  330.                  case 'std': $arr_type=MYSQLI_NUM; break;
  331.                  case 'inf': $arr_type=MYSQLI_ASSOC;break;
  332.                  default: $arr_type=MYSQLI_BOTH;break;
  333.               }
  334.              
  335.               $i=0;
  336.               while (($row=$result->fetch_array($arr_type)) && ($i<$num)) {
  337.                  $outz[]=$row;
  338.                  $i++;
  339.               }
  340.               $result->close();
  341.              
  342.               return($outz);
  343.              
  344.              
  345.            } else {
  346.               return false;
  347.            }           
  348.         }
  349.         //---------------------------------------- 
  350.         public function getQueryAll($insql, $param=1) {
  351.        
  352.            if (!$this->connected) return false;
  353.            
  354.            $this->mysqli_stmt=$this->mysqli->stmt_init();
  355.            
  356.            if (!$this->mysqli_stmt->prepare($insql)) {
  357.            
  358.               $this->logt("Error preparing $insql");
  359.               return false;
  360.            }
  361.            
  362.            
  363.            $this->mysqli_stmt->bind_param("i",$param_var);
  364.            $tmparr=array();
  365.              
  366.            if (is_array($param)) {
  367.              
  368.               foreach ($param as $param_var) {
  369.                  
  370.                  $this->mysqli_stmt->execute();
  371.                  $this->mysqli_stmt->bind_result($col1, $col2);
  372.                  
  373.                  while ($this->mysqli_stmt->fetch()) {
  374.                     $tmparr[]=array($col1, $col2);
  375.                  }
  376.                  
  377.                  //$result=$this->mysqli_stmt->get_result();   
  378.                  /*$tmparr=array_merge($tmparr,
  379.                         $result->fetch_all(MYSQLI_BOTH));*/
  380.                    
  381.                  }
  382.               }
  383.               else {
  384.                  $param_var=$param;
  385.                  $this->mysqli_stmt->execute();
  386.                  $this->mysqli_stmt->bind_result($col1,$col2);
  387.                  
  388.                  while ($this->mysqli_stmt->fetch()) {
  389.                     $tmparr[]=array($col1,$col2);
  390.                  }
  391.                  
  392.               }
  393.              
  394.            $this->mysqli_stmt->close();
  395.            return $tmparr;
  396.              
  397.         }
  398.        
  399.  
  400.  
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement