EIOS/integration/inc/class_DB.php

116 lines
4.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class class_DB{
private $mysqli;
function __construct($database,$user,$password='',$host='127.0.0.1'){
$this->mysqli = new mysqli($host, $user, $password, $database);
if (mysqli_connect_error()) {
die('Ошибка подключения (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
}
$this->debugSql=array();
}
/*
ЗАПРОС С ВЫБОРКОЙ И ВОЗВРАТ МАССИВА, 0 ЭЛЕМЕНТ КОЛ-ВО СТРОК
*/
function QUR_SEL($sql){
$out = array();
$keyd = $this->debug_sql($sql,1);
$qur = $this->mysqli->query($sql);
$this->debug_sql($sql,2,$keyd);
if ($qur){
$kol = $qur->num_rows;
$out['err'] = false;
$out['kol'] = $kol;
if ($kol){
while($rez = $qur->fetch_assoc()){
$out['rez'][] = $rez;
}
}
}else {
$out['err'] = true;
$out['sql']=$sql;
$out['rep']='ОШИБКА БД!!! <br/>'.$sql.'<br />('.$this->mysqli->errno.') '.$this->mysqli->error;
}
return $out;
}
/*
ЗАПРОС К БАЗЕ ДАННЫХ, И ВОЗВРАТ РЕЗУЛЬТАТА ЗАПРОСА
*/
function QUR($sql){
$out = array();
$qur = $this->mysqli->query($sql);
if ($qur){
$out['err']=false;
$out['id']=$this->lastinsertID();
}else{
$out['err']=true;
$out['sql']=$sql;
$out['id']=0;
$out['rep']='ОШИБКА БД!!! <br/>'.$sql.'<br />('.$this->mysqli->errno.') '.$this->mysqli->error;
//echo '<pre>'.print_r($out,1).'</pre>';
$this->log($out);
}
return $out;
}
function rescape($param){
return $this->mysqli->real_escape_string($param);
}
function lastinsertID(){
return $this->mysqli->insert_id;
}
function navpage($sql,$pole='id',$pkol=20){
$out = array();
$sql = preg_replace('/SELECT(.*)FROM/is','SELECT count('.$pole.') as kol FROM',$sql);
$rez = $this->QUR_SEL($sql);
//echo $sql.'<pre>'.print_r($rez,1).'</pre>';exit();
if(!$rez['err']) {
if(strpos($sql,'GROUP')!==false){
$out['all'] = $rez['kol'];
}else {
$out['all'] = $rez['rez'][0]['kol'];
}
$out['pages'] = ceil($out['all'] / $pkol);
}
else {
$out['all'] = 0;
$out['pages'] = 0;
}
return $out;
}
function log($str){
$msg = date('H:i:s d.m.Y').'::'.$_SESSION['user']['id'].'::'.$_SESSION['user']['fio'].'::';
$msg .= print_r($str,1)."\n";
file_put_contents('dblog.txt',$msg,FILE_APPEND);
}
function debug_sql($sql,$t,$keyd=''){
//$file = 'debug_sqls2.csv';
////if(file_exists($file)) unlink($file);
//$key = md5(time().$sql);
////if(!isset($this->debugSql[$key])) {
//if($t==1){
// $this->debugSql[$key]['sql'] = $sql;
// $this->debugSql[$key]['times'][] = round(microtime(),3);
// return $key;
//}
//if($t==2){
// $last = round(microtime(),3);
// $this->debugSql[$keyd]['times'][] = $last;
// $this->debugSql[$keyd]['time'] = $last - $this->debugSql[$keyd]['times'][0];
// $m1=array("\n","\r"); $m2=array(" "," ");
// $str = date('H:i:s d.m.Y').';'.round($this->debugSql[$keyd]['time'],2).';"'.str_replace($m1,$m2,$sql).'";';
// $str .= '"'.$_SERVER['REMOTE_ADDR'].'";';
// $str .= '"'.$_SERVER['REQUEST_METHOD'].'";';
// $str .= '"'.$_SERVER['PHP_SELF'].'";';
// $str .= '"'.$_SERVER['argv'].'";';
// $debug_backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// unset($debug_backtrace[0]);
// $str .= '"'.json_encode($debug_backtrace,JSON_UNESCAPED_UNICODE).'";';
// $str .= '"'.json_encode($_POST,JSON_UNESCAPED_UNICODE).'";';
// file_put_contents($file,$str."\n",FILE_APPEND);
// unset($this->debugSql[$keyd]);
//}
}
}
?>