<?php
/*
 * Created on 2012-4-6
 * Class mysql
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once("inc_config.php"); 						//主机配置文件
include_once("inc_function.php"); 						//常用函数调用
class mysql{
	//私有设置的变量
   	private $host;
   	private $name;
   	private $pass;
	private $table;
	private $ut;
	//初始化内容
	function __construct($host,$name,$pass,$table,$ut){
		$this->host=$host;
		$this->name=$name;
		$this->pass=$pass;
		$this->table=$table;
		$this->ut=$ut;
		$this->connect();
	}
	//链接数据库
   	function connect(){
	   	$link = mysql_connect($this->host,$this->name,$this->pass) or die("MYSQL链接不成功");
	   	mysql_select_db($this->table,$link) or die ("不存在数据库：".$this->table);
	   	mysql_query("SET NAMES '$this->ut'");
   	}
   	//常用的一些链接
   function query($sql, $type = '') {
	    if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
	    return $query;
	}
   	function error(){
   		return mysql_error();
   	}
   	function close() {
		return mysql_close();
	}
    function show($message = '', $sql = '') {
		if(!$sql) echo $message;
		else echo $message.'<br>'.$sql;
	}
    function affected_rows() {
		return mysql_affected_rows();
	}

	function result($query, $row) {
		return mysql_result($query, $row);
	}

	function num_rows($query) {
		return @mysql_num_rows($query);
	}

	function num_fields($query) {
		return mysql_num_fields($query);
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		return mysql_insert_id();
	}

	function fetch_row($query) {
		return mysql_fetch_row($query);
	}
	function fetch_array($query) {
		return mysql_fetch_array($query);
	}

	function version() {
		return mysql_get_server_info();
	}
   	//添加
	function fn_insert($table,$name,$value){
    	$this->query("insert into $table ($name) values ($value)");
    }
    //删除
	function fn_delete($table,$value){
    	$this->query("DELETE FROM $table WHERE ($value)");
    }
	//上一主题 下一主题函数
	function backid($id,$tables) {
		//$sql="select * from $tables where id < $id order by id desc limit 0,1";
		$rows=$this->query("select * from $tables where Id < $id order by id desc limit 0,1");
		$result=$this->fetch_array($rows);
		if(is_array($result)){
			return $result['Id'];
		}else{
			return 0;
		}
	}

	function nextid($id,$tables) {
		//$sql="select * from $tables where id > $id order by id asc limit 0,1";
		$rows=$this->query("select * from $tables where Id > $id order by id asc limit 0,1");
		$result=$this->fetch_array($rows);
		if(is_array($result)){
			return $result['Id'];
		}else{
			return 0;
		}
	}
}



 Class ShowPage{
 	//读数据库
 	function dbselect($table,$where=''){
 	global $sql;
 	return $sql = "select * from $table $where ";
 	}
	//翻页函数
	function gzpage($pagenum,$url){
/////////////////////////////////////////
	global $sql,$total,$pages;
	$page=$_GET['page']?$_GET['page']:1;
	$result=mysql_query($sql);
	$total=mysql_num_rows($result);
	$pages=ceil($total/$pagenum);
	$start=($page-1)*$pagenum;
	$sql.=" limit $start,$pagenum ";
//////////////////////////////////////
	$page=$_GET['page']?$_GET['page']:1;
	$str="<div class=zzPage><ul><li>共<B>$total</B>个 总<B>$pages</B>页 第<B>$page</B>页</li>";
	$str.="<li><a href='$url?page=1'>首页</a></li> ";
	if($page>1){
	$pre=$head."page=".($page-1);
	$str.="<li><a href='$url?$pre'>上一页</a></li> ";
	}else{
	$str.="<li>上一页</li> ";
	}
	if($page<$pages){
	$next=$head."page=".($page+1);
	$str.="<li><a href='$url?$next'>下一页</a></li> ";
	}else{
	$str.="<li>下一页</li> ";
	}
	$str.="<li><a href='$url?page=$pages'>末页</a></li></ul></div>";
	return $str;
	}
}
$db = new mysql($dbhost,$dbuser,$dbpass,$dbname,$charset);
?>
