Posted by & filed under Programming.

经过测试,上篇发布的数据库操作代码,在数据库连接上效率出现很大的问题,原因是过多的使用了try……catch语句,消耗了更多的内存资源,经过优化,其代码如下:

<?php
/**
 * 数据库操作类
 */
 
class MySql {
 
	var $user,$pass,$host,$db;
	var $id,$data,$fields,$row,$row_num,$insertid,$version,$query_num=0;
 
	function MySql($host,$user,$pass,$db)
	{
		$this->host = $host;
		$this->pass = $pass;
		$this->user = $user;
		$this->db = $db;
		$this->dbconnect($this->host, $this->user, $this->pass);
		$this->selectdb($this->db);
 
	}
	function dbconnect($host,$user,$pass)
	{
		$this->id = @ mysql_connect($host,$user,$pass) OR
		sysMsg("连接数据库失败,可能是mysql数据库用户名或密码错误");
	}
	function selectdb($db)
	{
		@ mysql_select_db($db,$this->id) OR sysMsg("未找到指定数据库");
	}
 
	function query($sql)
	{
		$query = @ mysql_query($sql,$this->id)
		OR sysMsg("SQL语句执行错误:$sql <br />".$this->geterror());
		$this->query_num();
		return $query;
	}
 
	function fetch_array($query)
	{
		$this->data = @mysql_fetch_array($query);
		return $this->data;
	}
	function query_num()
	{
		$this->query_num++;
	}
	function num_fields($query)
	{
		$this->fields = @mysql_num_fields($query);
		return $this->fields;
	}
	function fetch_row($query)
	{
		$this->row = @mysql_fetch_row($query);
		return $this->row;
	}
 
	function num_rows($query)
	{
		$this->row_num = @mysql_num_rows($query);
		return $this->row_num;
	}
 
	function insert_id()
	{
		$this->insertid = mysql_insert_id();
		return $this->insertid;
	}
 
	function fetch_one_array($sql)
	{
		$query = $this->query($sql);
		$this->data = $this->fetch_array($query);
		return $this->data;
	}
	function geterror()
	{
		return mysql_error();
	}
}
 
?>

Leave a Reply

  • (will not be published)