<?php
# Name: PHP版程氏音乐CMS管理系统 v3.0
# Author: 程氏<web@chshcms.com> [QQ:848769359]
# Homepage:http://www.chshcms.cn/

require_once("cs_config.php");
require_once("cs_config_user.php");
require_once("cs_db.php");
class cls_mysql
{

protected $link_id;

	public function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'gbk')
	{
		if(!($this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw)))
		{
			$this->ErrorMsg("Can't pConnect MySQL Server!");
		}
		
		@mysql_query("SET NAMES " . $charset, $this->link_id);
		
		if ($dbname)
		{
			if (@mysql_select_db($dbname, $this->link_id) === false )
			{
				$this->ErrorMsg("Can't select MySQL database($dbname)!");
				return false;
			}
			else
			{
				return true;
			}
		}

	}

	public function select_database($dbname)
	{
		return mysql_select_db($dbname, $this->link_id);
	}
	
	public function list_tables($dbname){
		return mysql_list_tables($dbname,$this->link_id);
		}
	
	public function list_fields($dbname,$tbname){
		return  mysql_list_fields($dbname,$tbname,$this->link_id);
		}
	
	public function fetch_array($query, $result_type = MYSQL_ASSOC)
	{
		return mysql_fetch_array($query, $result_type);
	}
	
	public function query($sql)
	{
		return mysql_query($sql, $this->link_id);
	}
	
	public function affected_rows()
	{
		return mysql_affected_rows($this->link_id);
	}
	
	public function num_rows($query)
	{
		return mysql_num_rows($query);
	}
	
	public function insert_id()
	{
		return mysql_insert_id($this->link_id);
	}

	public function selectLimit($sql, $num, $start = 0)
	{
		if ($start == 0)
		{
			$sql .= ' LIMIT ' . $num;
		}
		else
		{
			$sql .= ' LIMIT ' . $start . ', ' . $num;
		}
		return $this->query($sql);
	}

	public function getOne($sql, $limited = false)
	{
		if ($limited == true)
		{
			$sql = trim($sql . ' LIMIT 1');
		}
		
		$res = $this->query($sql);
		if ($res !== false)
		{
			$row = mysql_fetch_row($res);
		
			return $row[0];
		}
		else
		{
			return false;
		}
	}
	public function getrow($sql)
	{
		$res = $this->query($sql);
		if ($res !== false)
		{
			return mysql_fetch_assoc($res);
		}
		else
		{
			return false;
		}
	}

	public function getAll($sql)
	{
		$res = $this->query($sql);
		if ($res !== false)
		{
			$arr = array();
			while ($row = mysql_fetch_assoc($res))
			{
				$arr[] = $row;
			}
		
			return $arr;
		}
		else
		{
			return false;
		}
	}
	
	function ErrorMsg($message = '', $sql = '')
	{
		if ($message)
		{
			echo "<b>error info</b>: $message\n\n";
		}
		else
		{
			echo "<b>MySQL server error report:";
			print_r($this->error_message);
		}
		
		exit;
	}	

}
    //连接数据库开始
	$db  = new cls_mysql(CS_Sqlserver,CS_Sqluid,CS_Sqlpwd,CS_Sqlname,CS_Sqlcharset);

