<?php
function db_connect(){
global $SET_DB;
global $con;
	$con = mysqli_connect( 
            $SET_DB["host"],
            $SET_DB["user"],
            $SET_DB["password"],
            $SET_DB["db_name"]);

	if ($con == false)
		die("Невозможно подключиться к базе данных. Код ошибки: %s\n". mysqli_connect_error());
mysqli_query($con,"SET NAMES cp1251");
return $con;
mysql_query("set names 'cp1251'");
mysql_query ("set character_set_client='cp1251'");
mysql_query ("set character_set_results='cp1251'");
mysql_query ("set collation_connection='cp1251_general_ci'");
}


function db_string($result){
global $con;
	$result = mysqli_real_escape_string($con, $result);	
return $result;
}

function db_query($query){
global $con;
	$result = mysqli_query($con, $query);
	
	if ($result == false)
		die('Ошибка запроса: '.mysqli_error($con).' Код ошибки: '.mysqli_errno($con));
		
return $result;
}

function db_one_query($query){
global $con;
db_connect();
$res = db_query($query);
db_close();

return $res;
}


function db_assoc($result){
	$row = mysqli_fetch_assoc($result);	
return $row;
}

function db_rows($result){
	$row = mysqli_num_rows($result);	
return $row;
}


function db_close(){
global $con;
	mysqli_close($con);
}

function db_last_id(){
global $con;
return	mysqli_insert_id($con);
}
?>