<?php
if (!defined("FUNC_FILE")) die("Illegal File Access");
class sql_db {
var $db_connect_id;
var $query_result;
var $row = array();
var $rowset = array();
var $num_queries = 0;
var $total_time_db = 0;
var $time_query = "";
# Îòêðûâàåò ñîåäèíåíèå ñ ñåðâåðîì MySQL
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $charset = false) {
$this->db_connect_id = @mysqli_connect($sqlserver, $sqluser, $sqlpassword, $database);
#if ($charset) @mysqli_set_charset($charset);
if ($this->db_connect_id) {
/*
if ($database != "" && !@mysqli_select_db($database)) {
@mysqli_close($this->db_connect_id);
$this->db_connect_id = false;
}
*/
return $this->db_connect_id;
} else {
return false;
}
}
# Çàêðûâàåò ñîåäèíåíèå ñ ñåðâåðîì MySQL
function sql_close() {
if ($this->db_connect_id) {
if ($this->query_result) @mysqli_free_result($this->query_result);
$result = @mysqli_close($this->db_connect_id);
return $result;
} else {
return false;
}
}
# Âûïîëíÿåò SQL çàïðîñ
function sql_query($query = "") {
if ($this->query_result) unset($this->query_result);
if ($query != "") {
$st = array_sum(explode(" ", microtime()));
$this->query_result = @mysqli_query($this->db_connect_id, $query);
$total_tdb = round(array_sum(explode(" ", microtime())) - $st, 5);
$this->total_time_db += $total_tdb;
$color = ($total_tdb > 0.01) ? "sl_red" : "igreen";
$this->time_query .= "<span class=\"".$color."\">".$total_tdb."</span> "._SEC.". => [".$query."]";
}
if ($this->query_result) {
$this->time_query .= "<br>";
$this->num_queries += 1;
unset($this->row[$this->query_result]);
unset($this->rowset[$this->query_result]);
return $this->query_result;
} else {
$this->time_query .= " => <span class=\"sl_red\">"._ERROR.": ".mysqli_errno()." - ".mysqli_error()."</span><br>";
if (function_exists("error_sql_log")) error_sql_log(@mysqli_errno($this->db_connect_id), @mysqli_error($this->db_connect_id), $query);
return false;
}
}
# Âîçâðàùàåò êîëè÷åñòâî ñòðîê â ðåçóëüòàòå
function sql_numrows($query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
return ($query_id) ? @mysqli_num_rows($query_id) : false;
}
# Ïîëó÷àåò êîëè÷åñòâî ðÿäîâ, çàäåéñòâîâàííûõ â ïðåäûäóùåé îïåðàöèè MySQL
function sql_affectedrows() {
return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
}
# Âîçâðàùàåò êîëè÷åñòâî ñòîëáöîâ äëÿ ïîñëåäíåãî çàïðîñà
function sql_numfields($query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
return ($query_id) ? @mysqli_num_fields($query_id) : false;
}
# Âîçâðàùàåò íàçâàíèå óêàçàííîé êîëîíêè ðåçóëüòàòà çàïðîñà
function sql_fieldname($offset, $query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
return ($query_id) ? @mysqli_field_name($query_id, $offset) : false;
}
# Âîçâðàùàåò òèï óêàçàííîãî ïîëÿ ðåçóëüòàòà çàïðîñà
function sql_fieldtype($offset, $query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
return ($query_id) ? @mysqli_field_type($query_id, $offset) : false;
}
# Âîçâðàùàåò ðåçóëüòàò êàê ÷èñëåííûé ìàññèâ
function sql_fetchrow($query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
if ($query_id) {
$this->row[$query_id] = @mysqli_fetch_array($query_id);
return $this->row[$query_id];
} else {
return false;
}
}
# Âîçâðàùàåò ÷èñëåííûé è àññîöèàòèâíûé ìàññèâû
function sql_fetchrowset($query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
if ($query_id) {
unset($this->rowset[$query_id]);
unset($this->row[$query_id]);
while ($this->rowset[$query_id] = @mysqli_fetch_array($query_id)) $result[] = $this->rowset[$query_id];
return $result;
} else {
return false;
}
}
# Âîçâðàùàåò äàííûå èç óêàçàííîé ñòðîêè è ïîëÿ
function sql_fetchfield($field, $rownum = -1, $query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
if ($query_id) {
if ($rownum > -1) {
$result = @mysqli_result($query_id, $rownum, $field);
} else {
if (empty($this->row[$query_id]) && empty($this->rowset[$query_id])) {
if ($this->sql_fetchrow()) $result = $this->row[$query_id][$field];
} else {
if ($this->rowset[$query_id]) {
$result = $this->rowset[$query_id][0][$field];
} elseif ($this->row[$query_id]) {
$result = $this->row[$query_id][$field];
}
}
}
return $result;
} else {
return false;
}
}
# Ïåðåìåùåíèå óêàçàòåëÿ íà ïðîèçâîëüíóþ ñòðîêó â ðåçóëüòàòå çàïðîñà
function sql_rowseek($rownum, $query_id = 0) {
if (!$query_id) $query_id = $this->query_result;
return ($query_id) ? @mysqli_data_seek($query_id, $rownum) : false;
}
# Âîçâðàùàåò ID, ñãåíåðèðîâàííûé ïðè ïîñëåäíåì INSERT-çàïðîñå
function sql_nextid() {
return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
}
# Îñâîáîæäàåò ïàìÿòü îò ðåçóëüòàòà çàïðîñà
function sql_freeresult($query_id = 0){
if (!$query_id) $query_id = $this->query_result;
if ($query_id) {
unset($this->row[$query_id]);
unset($this->rowset[$query_id]);
@mysqli_free_result($query_id);
return true;
} else {
return false;
}
}
# Âîçâðàùàåò êîä è ñòðîêó îïèñàíèÿ îøèáêè ïîñëåäíåãî ñîåäèíåíèÿ
function sql_error($query_id = 0) {
$result["code"] = @mysqli_errno($this->db_connect_id);
$result["message"] = @mysqli_error($this->db_connect_id);
return $result;
}
}
?>