<?php
function db_connect()
{
$link = mysql_connect(db_host, db_user, db_pass) or $debug->error(mysql_error()."<br />$query","SQL Error");
mysql_select_db(db_name) or $debug->error(mysql_error()."<br />$query","SQL Error");
if (db_charset) mysql_query('SET NAMES '.db_charset);
return true;
}
function db_get_line($table = '', $conditions = '')
{
return db_query('SELECT * FROM '.db_get_prefix($table).($conditions ? ' WHERE '.$conditions : '').' LIMIT 1', true);
}
function db_get_lines($table = '', $conditions = '')
{
return db_query('SELECT * FROM '.db_get_prefix($table).($conditions ? ' WHERE '.$conditions : ''));
}
function db_get_count($table, $conditions = '')
{
$line = db_query('SELECT COUNT(*) FROM '.db_get_prefix($table).($conditions ? ' WHERE '.$conditions : ''), true, false);
return $line[0];
}
function db_delete_line($table, $conditions = '')
{
return db_query('DELETE FROM '.db_get_prefix($table).($conditions ? ' WHERE '.$conditions : '').' LIMIT 1');
}
/**
* Основная функция работы с базой
* @param string $query - запрос
* @param string $query - запрос
* @param string $first - возвращаем только первый элемент массива
* @return unknown_type
*/
function db_query($query = '', $first = false, $assoc = true)
{
db_connect();
//echo '<p>'.__LINE__.'<br/>'.$query.'</p>';
$resource = mysql_query($query);
if ($resource === true) return false;
$res = array();
if ($assoc)
{
while($line = mysql_fetch_assoc($resource))
{
$res[] = $line;
}
} else {
while($line = mysql_fetch_row($resource))
{
$res[] = $line;
}
}
mysql_free_result($resource);
if ($first)
{
return count($res) ? $res[0] : false;
}
return $res;
}
/**
* Функция получения префикса
* @param string $table - название таблицы
* @return string
*/
function db_get_prefix($table)
{
return db_prefix.$table;
}
function doquery($query, $table, $fetch = false)
{
global $dbsettings, $numqueries, $link, $debug;
if(!$link)
{
$link = mysql_connect($dbsettings['server'], $dbsettings['user'], $dbsettings['pass']) or $debug->error(mysql_error()."<br />$query","SQL Error");
//mysql_query("/*!40101 SET NAMES 'cp1251' */") or die("Error: " . mysql_error());
mysql_select_db($dbsettings["name"]) or $debug->error(mysql_error()."<br />$query","SQL Error");
echo mysql_error();
}
$sql = str_replace("{{table}}", $dbsettings["prefix"].$table, $query);
// $sql = addslashes($sql);
// $sql = str_replace("'", "", $sql);
// echo '<p>'.__LINE__.'<br/>'.$sql.'</p>';
$sqlquery = mysql_query($sql) or $debug->error(mysql_error()."<br/>$sql<br/>","SQL Error");
unset($dbsettings);
/*
$numqueries++;
$arr = debug_backtrace();
$file = end(explode('/',$arr[1]['file']));
$line = $arr[1]['line'];
$debug->add("<tr><th>Query $numqueries: </th><th>$query</th><th>$file($line)</th><th>$table</th><th>$fetch</th></tr>");
*/
if ($fetch)
{
return mysql_fetch_array($sqlquery);
}
else
{
return $sqlquery;
}
}
?>