View file mysql.php

File size: 3.32Kb
<?php
/*(с) Im !eee*/
$conn=false; $p_nm_cch=false; $lock=false;
function m_conn(){
	global $conn;
	($conn=mysql_connect(MY_HOST,MY_USER,MY_PASS)) or exit(ex_hn());
	mysql_select_db(MY_DB, $conn) or exit(ex_hn());
	mysql_query('SET NAMES "utf8"', $conn) or exit(ex_hn());
}
function lock($md){
	global $conn;
	if(!$conn){m_conn();} 
	if($md=='r'){
		$q=mysql_query('LOCK TABLE `'.MY_TBL.'` read', $conn);
	}else{
		$q=mysql_query('LOCK TABLE `'.MY_TBL.'` write', $conn);
	}
	global $lock; $lock=true;
	if(!$q){exit(ex_hn());}
}
function rel_lock($md){
	global $conn,$lock;
	if(!$lock){return;}
	if(!$conn){return;}
	mysql_query('UNLOCK TABLES', $conn) or exit(ex_hn());
}

function m_on_p(&$p, &$l){
	global $conn;
	if(!$conn){m_conn();}
	$p=(int)$p; $l=0;
	$ret=array(array(), array(), array());
	$mp=p_num();
	if($p>$mp||$p<1){$p=$mp;}
	($q=mysql_query('SELECT `auth`,UNIX_TIMESTAMP(`date`),`post` FROM `'.MY_TBL.'` ORDER BY `id` LIMIT '.($p-1)*MESS_TOT.','.MESS_TOT, $conn)) or exit(ex_hn());
	$l=mysql_num_rows($q);
	while($arr=mysql_fetch_row($q)){
		$ret[0][]=$arr[0];
		$ret[1][]=$arr[1];
		$ret[2][]=$arr[2];
	}
	return $ret;
}

function p_num(){
	global $p_nm_cch,$conn;
	if($p_nm_cch!==false){return $p_nm_cch;}
	if(!$conn){m_conn();}
	($q=mysql_query('SELECT COUNT(`id`) FROM `'.MY_TBL.'`',$conn)) or exit(ex_hn());
	$p_nm_cch=ceil(mysql_result($q,0)/MESS_TOT);
	return $p_nm_cch;
}

function last_wr(){
	global $conn;
	if(!$conn){m_conn();}
	($q=mysql_query('SELECT max(`id`) FROM `'.MY_TBL.'`',$conn)) or exit(ex_hn());
	$id=mysql_result($q,0);
	($q=mysql_query('SELECT UNIX_TIMESTAMP(`date`) FROM `'.MY_TBL.'` WHERE `id`='.$id,$conn)) or exit(ex_hn());
	return time()-mysql_result($q,0);
}

function wr($n, $m){
	global $conn;
	if(!$conn){m_conn();}
	mysql_query('INSERT INTO `'.MY_TBL.'`(`auth`,`post`) VALUES ("'.mysql_real_escape_string($n).'","'.mysql_real_escape_string($m).'")', $conn) or exit(ex_hn());
}

function get_mes($p, $n){
	global $conn;
	if(!$conn){m_conn();}
	$p=(int)$p; $n=(int)$n;
	$q=mysql_query('SELECT `auth`,UNIX_TIMESTAMP(`date`),`post` FROM `'.MY_TBL.'` ORDER BY `id` LIMIT '.(($p-1)*MESS_TOT+$n).',1', $conn);
	if(!$q||mysql_num_rows($q)==0){
		exit(ex_hn('Сообщение не найдено'));
	}
	return mysql_fetch_row($q);
}

function ed_m($p, $n, $a, $d, $m){
	global $conn;
	if(!$conn){m_conn();}
	$p=(int)$p; $n=(int)$n;
	$q=mysql_query('SELECT `id` FROM `'.MY_TBL.'` ORDER BY `id` LIMIT '.(($p-1)*MESS_TOT+$n).',1', $conn);
	if(!$q||mysql_num_rows($q)==0){
		exit(ex_hn('Сообщение не найдено'));
	}
	$id=mysql_result($q, 0);
	mysql_query('UPDATE `'.MY_TBL.'` SET `auth`'.'="'.mysql_real_escape_string($a).'", `date`=FROM_UNIXTIME('.$d.'), `post`="'.mysql_real_escape_string($m).'" WHERE `id`='.$id, $conn) or exit(ex_hn());
}

function del_m($p,$n){
	global $conn;
	if(!$conn){m_conn();}
	$p=(int)$p; $n=(int)$n;
	$q=mysql_query('SELECT `id` FROM `'.MY_TBL.'` ORDER BY `id` LIMIT '.(($p-1)*MESS_TOT+$n).',1', $conn);
	if(!$q||mysql_num_rows($q)==0){
		exit(ex_hn('Сообщение не найдено'));
	}
	$id=mysql_result($q, 0);
	mysql_query('DELETE FROM `'.MY_TBL.'` WHERE `id`='.$id, $conn) or exit(ex_hn());
}

function ct_all(){
	$t=mysql_connect(MY_HOST,MY_USER,MY_PASS);
	if(!($t&&mysql_select_db(MY_DB,$t)&&($q=mysql_query('SELECT count(*) FROM `'.MY_TBL.'`', $t)))){return 0;}
	return mysql_result($q, 0);
}
?>