pnd8_rasp/app/engine/core/db.class.php

125 lines
2.5 KiB
PHP

<?php
class db {
var $database;
var $r;
var $host;
var $user;
var $pass;
var $dbname;
var $lastRow;
function connect( $host, $user, $pass, $dbname, $db_create = false ) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbname = $dbname;
$this->database = mysqli_connect($host,$user,$pass,$dbname);
if ( !$this->database ) {
die('Mysql server fail');
}
}
function new_db() {
$db = clone $this;
$db->connect($this->host, $this->user, $this->pass, $this->dbname);
return $db;
}
function q( $sql ) {
global $statistica;
$statistica['q_count']++;
$statistica['sqls'][] = $sql;
$starttime = microtime( true );
if (!($result = mysqli_query($this->database, $sql) ) ) {
if (php_sapi_name()==='cli') {
echo '/* MYSQL_ERROR */ ' . PHP_EOL;
echo mysqli_error($this->database);
echo PHP_EOL;
die();
}
if ( _DEBUG != true ) {
header("HTTP/1.1 503 Service Unavailable", true, 503);
header("Retry-After: 3600");
$data = file_get_contents(_SHOP_DIR . 'blocker/index.html');
echo $data;
die();
}
//die('error');
echo "<xmp>";
debug_print_backtrace();
echo "</xmp>";
die(mysqli_error($this->database) . "<br>" . $sql );
} else {
$endtime = microtime( true );
$statistica['query_time'] += ($endtime - $starttime);
$statistica['sql_time'][] = ($endtime - $starttime);
$this->r = $result;
return clone $this;
}
}
function r() {
$row = mysqli_fetch_assoc($this->r);
if ( $row ) {
$this->lastRow = $row;
return true;
} else {
return false;
}
}
function nr() {
return $this->r();
}
function f( $key ) {
return $this->lastRow[$key];
}
function insert_id() {
return mysqli_insert_id($this->database);
}
function affected_rows() {
return mysqli_affected_rows($this->database);
}
function close() {
mysqli_close( $this->database );
}
public function fetchAll() {
return mysqli_fetch_all($this->r, MYSQLI_ASSOC);
}
}
$core_db = new db();
function db_escape_string($str = "") {
global $core_db;
return mysqli_real_escape_string($core_db->database, $str);
}
function db_real_escape_string($str = "") {
return db_escape_string($str);
}
function mysql_real_escape_string($str = "") {
return db_escape_string($str);
}
?>