<?php
/**
 *类  名：MySql
 *文件名：class_mysql.php
 *编写人：http://Www.HuCart.Com
 */
class MySql
{	 //类开始
	 private $host_name;
	 private $port;
	 private $user_name;
	 private $pass_word;
	 private $data_base_name;
	 private $data_version;
	 //var $insert_id = "";
	 public $mysql_conncet = false;
	 var $info = array();
	 var $page_num = 1;
	 var $page_size = 30;
	 var $page_str = "";
	 var $result_count = 0;
	 var $file_name = "";
	 var $page_count = 1;
	 var $prefix = '';

/*****************************************************************************
函数名称：MySql($host_name = "localhost:3306", $user_name = "root", $pass_word = "", $data_base_name = "database")
函数作用：构造函数,初始化类数据
传入参数：$host_name      主机名和端口
		  $user_name      连接数据库用户名
		  $pass_word	     连接数据库密码
		  $data_base_name  数据库名
返 回 值：无
调用示列：$MySql = new MySql( $localhost_sql ,$user_sql , $pass_sql , $database_sql);初始化一个类
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/
    function __construct($host_name = "localhost", $user_name = "root", $pass_word = "", $data_base_name = "hucartdata",$port = '3306')
    {
        $this -> host_name = $host_name.':'.$port;
        $this -> user_name = $user_name;
        $this -> pass_word = $pass_word;
        $this -> data_base_name = $data_base_name;
		//$this -> port = $port;
    }

/*****************************************************************************
函数名称：connect_data()
函数作用：连接数据库(只在本类中使用)
传入参数：无
返 回 值：无
调用示列：$this -> connect_data()
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/
    function connect_data()
	{   
		if( $this-> mysql_conncet == false ){
			@$this -> mysql_conncet = mysql_connect($this -> host_name, $this -> user_name, $this -> pass_word)  or die( 'mysql_error_parameter' ) ; //or die( mysql_error())
					 
			$GLOBALS['charset'] = 'utf8';
			if($this->mysql_version() > '4.1' && $GLOBALS['charset']){
				 mysql_query("SET character_set_connection=".$GLOBALS['charset'].", character_set_results=".$GLOBALS['charset'].", character_set_client=binary");
			}
			
			if($this->mysql_version() > '5.0'){
				 mysql_query("SET sql_mode=''");
			}

			mysql_select_db($this -> data_base_name, $this -> mysql_conncet)  or die( 'mysql_error_parameter' ) ; // or die( mysql_error() )
		}
    }


/*****************************************************************************
函数名称：select_info($sql_query, $type_mysql = "MYSQL_ASSOC")
函数作用：执行SQL查询语句
传入参数：$sql_query SQL语句
		  $type_mysql = "MYSQL_ASSOC" 返回值数组类型控制  "MYSQL_ASSOC"将返回表字段为数组索引
返 回 值：true / false 
调用示列：$isTrue   = $this ->  select_info($sql_query);
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/
    function select_info($sql_query, $type_mysql = "MYSQL_ASSOC")
    {
	    $this -> connect_data();
	    $this -> query = mysql_query($sql_query, $this -> mysql_conncet)  or die( 'mysql_error_parameter' ) ; /// or die ( mysql_error())
        $result = array();
	   
	    if($type_mysql == "MYSQL_NUM")
	    {
		   while($row = mysql_fetch_array( $this -> query, MYSQL_NUM)) //MYSQL_NUM
		   {   $result[] = $row;  }
	     }else{
	 	   while($row = mysql_fetch_array( $this -> query, MYSQL_ASSOC) ) //MYSQL_ASSOC
		   {   $result[] = $row;   }
	     }
		 
	     return $this -> info = $result;
	     //return true;
    }
	

/*****************************************************************************
函数名称：select_one($sql_query, $type_mysql = "MYSQL_ASSOC")
函数作用：执行SQL查询语句
传入参数：$sql_query SQL语句
		  $type_mysql = "MYSQL_ASSOC" 返回值数组类型控制  "MYSQL_ASSOC"将返回表字段为数组索引
返 回 值：单一数据值
调用示列：$isTrue   = $this ->  select_one($sql_query);
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/
    function select_one($sql_query, $type_mysql = "MYSQL_ASSOC")
    {
	    $this -> connect_data();
	    $this -> query = mysql_query($sql_query, $this -> mysql_conncet)  or die( 'mysql_error_parameter' ); // or die ( mysql_error())
        //$result = array();
	   
	    if($type_mysql == "MYSQL_NUM")
	    {
		   $row = mysql_fetch_array( $this -> query, MYSQL_NUM);
		   //while($row = mysql_fetch_array( $this -> query, MYSQL_NUM)) //MYSQL_NUM
		   //{   
		   $result = $row;
		   //}
		 
	     }else{
		 	$row = mysql_fetch_array( $this -> query, MYSQL_ASSOC);
	 	   //while($row = mysql_fetch_array( $this -> query, MYSQL_ASSOC) ) //MYSQL_ASSOC
		   //{   
		   $result = $row;   
		   //}
	     }
		 
	     return $result; //sqlQuery
	     //return true;
    }
	
	
	
/*****************************************************************************
函数名称：count_info($table_name, $where_clause)
函数作用：统计记录总数（常用于分页）
传入参数：$table_name 表名
		  $where_clause SQL条件
返 回 值：无
调用示列：$this -> count_info($table_name, $where_clause);
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/
	function count_info($table_name, $where_clause , $count_sql = "" )
	{
	    if($count_sql!='')
		{
			$countSql =  ' select count( '.$count_sql.' )  from ' . $table_name;
		}else{
			$countSql =  ' select count(1) from ' . $table_name;
		}
		
	    if($where_clause != '')  $countSql .= ' where ' . $where_clause;

	    $this -> connect_data();
		$this -> query = mysql_query( $countSql, $this -> mysql_conncet)   or die( 'mysql_error_parameter' ) ;//or die ( mysql_error()); 
	    $this -> result_count = mysql_result($this -> query, 0)   or die( 'mysql_error_parameter' ) ; // or die ( mysql_error());
	
	}
	
/*****************************************************************************
函数名称：get_page_info($select_clause = " * ", $table_name , $where_clause = '', $group_clause = '' , $order_clause = '', $limit_clause = '', $offert_clause = 0 , $page_num = 1 , $page_size = 30 )
函数作用：获取单一页信息内容(分页程序)
传入参数：$select_clause  SQL语句中要查询的相关字段
		  $table_name     SQL查询表名
		  $where_clause   SQL条件语句
		  $group_clause   SQL排序方式(group by ""后语句)
		  $order_clause   SQL排序方式
		  $offert_clause = 0 SQL的offert后的语句
		  $page_num  	 当前页码  
		  $page_size      每页显示记录数
		  
返 回 值：true / false
调用示列：$isTrue = $MySql -> get_page_info($select_clause , $table_name , $where_clause , $group_clause = '' , $order_clause = '',  $offert_clause = 0 , $page_num  , $page_size );
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/
	function get_page_info($select_clause = " * ", $table_name , $where_clause = '', $group_clause = '' , $order_clause = '', $offert_clause = 0 , $page_num = 1 , $page_size = 30 ,$countSql = "" )
	{
		$num_begin    = ($page_num-1)*$page_size ;
	    $limit_clause = $num_begin.' , '.$page_size;

	    $sql_query = $this ->  build_select($select_clause , $table_name, $where_clause , $group_clause  , $order_clause , $limit_clause , $offert_clause );
		$isTrue   = $this ->  select_info($sql_query);
		
		$this ->  count_info($table_name, $where_clause , $countSql);
	    $this ->  build_page_str( $page_num , $page_size );
		
        return $isTrue;
	
	}
	
/*****************************************************************************
函数名称：build_page_str( $page_num = 1, $page_size = 30 )
函数作用：输入分页相关字符串(对应本类中分页显示)
传入参数：$page_num   当前页码 
		  $page_size  每页显示记录数
		  
返 回 值：无
调用示列：$this ->  build_page_str( $page_num , $page_size );
作    者: http://Www.HuCart.Com
创建日期：2007-09-27
*****************************************************************************/

	function build_page_str( $page_num = 1, $page_size = 30 )
	{

		$this -> page_count = ceil($this -> result_count / $page_size);
		$num_begin = ($page_num-1)*$page_size  + 1;
		$numEnd   = 0;
		if( ($page_num * $page_size) < ($this -> result_count) )  $numEnd = $page_num * $page_size;
		else  $numEnd = $this -> result_count;
	
		$this -> page_str  = " <div align='right'>  ";
		$this -> page_str .=  "  [第 <font color='#FF0000'>".$num_begin."</font> 至 <font color='#FF0000'>".$numEnd."</font> 条记录] ";
		$this -> page_str .=  "共 [<font color='#FF0000'>".($this -> result_count)."</font>] 记录   [ ";
	
		if( $page_num == 1)  $this -> page_str .=  "<font color='#999999'>首页  | 上一页</font>";  
		else  $this -> page_str .=  "<a href='javascript:getToPage(1)' class='aa'>首页</a>  |  <a href='javascript:getToPage(".($page_num-1).")' class='aa'>上一页</a> ";
		
		if( $page_num == ($this -> page_count) or (($this -> page_count) <= 0) )  $this -> page_str .=  " |<font color='#999999'> 下一页 | 尾页</font> ] ";
		else  $this -> page_str .=  " | <a href='javascript:getToPage(".($page_num+1).")' class='aa'>下一页</a>  |  <a href='javascript:getToPage(".($this -> page_count).")' class='aa'>尾页</a> ] ";
	
		
		$this -> page_str .=  "第 <select onchange = 'getToPage(this.value)'>";
		
		for($i = 1; $i <= $this -> page_count; $i++){  
		
			  if($page_num == $i){
				$this -> page_str .= "<option value='".$i."' selected >".$i."</option>";
			  }else{
				$this -> page_str .= "<option value='".$i."'>".$i."</option>";
			  }
		}
		
		$this -> page_str .=  "</select>页&nbsp;&nbsp;</div>";
	}

/*****************************************************************************
函数名称：build_select($select_clause = '*', $table_name, $where_clause = '', $group_clause = '' , $order_clause = '', $limit_clause = '', $offert_clause = 0)
函数作用：生成查询SQL语句
传入参数：$select_clause 查询字段
          $table_name    数据表名
          $where_clause  查询条件
          $group_clause  GROUP BY字段
          $order_clause  排序字段
          $limit_clause  记录数
          $offert_clause 起始记录（偏移量）
返回值：  $sql (返回select查询SQL语句)
调用示列：$sql_query = $this ->  build_select($select_clause , $table_name, $where_clause , $group_clause  , $order_clause , $limit_clause , $offert_clause );
作    者: http://Www.HuCart.Com
创建日期：2006-09-08
*****************************************************************************/
	
	function build_select($select_clause = '*', $table_name, $where_clause = '', $group_clause = '' , $order_clause = '', $limit_clause = '', $offert_clause = 0)
	{
		if($table_name != '') 
		{ 
		  $sql = 'SELECT ' . $select_clause . ' FROM ' . $table_name;
		 } else {
		  die("非法操作.");
		 }
		
		if($where_clause != '') $sql .= ' WHERE ' . $where_clause;
		if($group_clause != '') $sql .= ' GROUP BY ' . $group_clause;
		if($order_clause != '') $sql .= ' ORDER BY ' . $order_clause;
		if($limit_clause != '') $sql .= ' LIMIT  ' .	  $limit_clause;
		if($offert_clause != 0) $sql .= ' OFFSET ' .  $offert_clause;
		//echo $sql;
		return $sql;
	}


/*****************************************************************************
函数名称：query_sql($sql_query)
函数作用：执行SQL语句
传入参数：$sql_query 要执行的SQL语句
返回值：  true / false
调用示列：$this -> query_sql($sql_query);
作    者: http://Www.HuCart.Com
创建日期：2006-09-08
*****************************************************************************/
  
    function query_sql($sql_query)
    {
	  $this -> connect_data();
	  $this -> query = mysql_query($sql_query, $this -> mysql_conncet)  or die( 'mysql_error_parameter' ) ; //or die ( mysql_error())
        return true;
     }

/*****************************************************************************
函数名称：insert_sql($sql_query)
函数作用：添加一条记录（可以获得刚添加记录的ID）
传入参数：$sql_query 要执行的SQL语句
返回值：  true / false
调用示列：$this -> insert_sql($sql_query);
作    者: http://Www.HuCart.Com
创建日期：2006-09-08
*****************************************************************************/
    function insert_sql($sql_query)
    {
	  $this -> connect_data();     
	  $this -> query = mysql_query($sql_query, $this -> mysql_conncet)  or die( 'mysql_error_parameter' ) ; //or die ( mysql_error())
	  return  mysql_insert_id(); // $this -> insert_id  =  mysql_insert_id
      //  return true;
     }


	function mysql_version() {
		if(empty($this->data_version)) {
			$this->data_version = mysql_get_server_info();
		}
		return $this->data_version;
	}

/*****************************************************************************
函数名称：__destruct()
函数作用：关闭数据库连接
传入参数：无
返回值：  无
调用示列：$MySql->close_sql();
作    者: http://Www.HuCart.Com
创建日期：2006-09-08

	 function close_sql()
	 {
		if( $this -> mysql_conncet != false ){
	    mysql_close();
		}
	 }

*****************************************************************************/

	 
	 function __destruct()
	 {
		//var_dump($this -> mysql_conncet);
		if( $this -> mysql_conncet != false ){
	    	//mysql_close();
		}
	 }
//类结束
}
