5 创建类数据库连接
5.1 建立cls_mysql.php类文件
复制代码 代码如下:
<?php
class cls_mysql
{
protected $link_id;
function __construct($dbhost,$dbuser,$dbpw,$dbname='''',$charset=''GB2312'')
{
if(!($this->link_id=mysql_connect($dbhost,$dbuser,$dbpw)))
{
$this->ErrorMsg("Can''t pConnect MySQL Server($dbhost)!");
}
mysql_query("SET NAMES ".$charset,$this->link_id);
if($dbname)
{
if(mysql_select_db($dbname,$this->link_id)===false)
{
$this->ErrorMsg("Can''t slect MYSQL database($dbname)!");
return false;
}
else
{
return true;
}
}
}
public function select_database($dbname)
{
return mysql_select_db($dbname,$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_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