<?php
class DB
{
private $conn='';
private static $param=array(
'host'=>'localhost',
'username'=>'root',
'password'=>'111111',
'ispconnect'=>false,
'dbname'=>'zhuanti'
);
function __construct()
{
$this->connect();
}
public function connect()
{
try
{
if(self::$param['ispconnect'])
{
$this->conn=@mysql_pconnect(self::$param['host'],self::$param['username'],self::$param['password']);
}
else
{
$this->conn=@mysql_connect(self::$param['host'],self::$param['username'],self::$param['password']);
}
if(!$this->conn)
{
throw new Exception("数据库链接失败");
}
else
{
mysql_query("set names 'gbk'");
}
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
try
{
if(!@mysql_select_db(self::$param['dbname'],$this->conn))
{
throw new Exception("数据库不存在,请联系管理员");
}
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
}
/*操作数据库*/
public function execute($sql="")
{
if(empty($sql)) return false;
if(empty($this->conn)) return false;
try
{
$results=mysql_query($sql,$this->conn);
if(!$results)
{
throw new Exception("数据库操作失败");
}
else
{
return $results;
}
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
}
/*查询数据库*/
public function getArray($sql="")
{
$this->sql=$sql;
$rs=$this->execute($this->sql);
$cont=0;
$data=array();
while($row=mysql_fetch_array($rs))
{
$data[$cont]=$row;
$cont++;
}
mysql_free_result($rs);
return $data;
}
/*查询记录数*/
public function getNum($sql="")
{
$this->sql=$sql;
$rs=$this->execute($this->sql);
if(!$num=mysql_num_rows($rs))
{
echo '数据库中没有数据';
}
else
{
mysql_free_result($rs);
return $num;
}
}
/*插入数据*/
public function getInsert($sql="")
{
$this->sql=$sql;
return $this->execute($this->sql);
}
/*获得插入数据的ID*/
public function getLastId($sql="")
{
$this->sql=$sql;
$rs=$this->execute($this->sql);
return mysql_insert_id();
}
/*数据更新*/
public function getUpdate($sql="")
{
$this->sql=$sql;
return $this->execute($this->sql);
}
/*数据删除*/
public function getDel($sql='')
{
$this->sql=$sql;
return $this->execute($this->sql);
}
public function getClose()
{
mysql_close($this->conn);
}
}
?>
<?php
class DB
{
private $conn='';
private static $param=array(
'host'=>'localhost',
'username'=>'root',
'password'=>'111111',
'ispconnect'=>false,
'dbname'=>'cms'
);
function __construct()
{
if(self::$param['ispconect'])
{
$this->conn=@mysql_pconnect(self::$param['host'],self::$param['username'],self::$param['password']);
}
else
{
$this->conn=@mysql_connect(self::$param['host'],self::$param['username'],self::$param['password']);
}
@mysql_query("set names 'gbk'");
}
/*数据库操作*/
public function execute($sql)
{
try
{
if(!$this->conn)
{
throw new Exception("数据库连接失败");
}
else
{
try
{
if(!mysql_select_db(self::$param['dbname'],$this->conn))
{
throw new Exception("数据出选择错误,请联系管理员");
}
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
$results=mysql_query($sql,$this->conn);
try
{
if(!$results)
{
throw new Exception("数据库操作失败");
}
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
return $results;
}
/*数据插入,修改,删除*/
public function executeUpdate($sql="")
{
$this->sql=$sql;
if(empty($this->sql)) return false;
return $this->execute($this->sql);
}
/*数据库查询*/
public function getSelect($sql="")
{
$this->sql=$sql;
if(empty($this->sql)) return false;
$rs=$this->execute($this->sql);
if(!$rs)
{
return false;
}
$cont=0;
$data=array();
while($row=mysql_fetch_array($rs))
{
$data[$cont]=$row;
$cont++;
}
mysql_free_result($rs);
return $data;
}
/*查询数据库记录数*/
public function getNum($sql="")
{
$this->sql=$sql;
if(empty($this->sql)) return false;
$rs=$this->execute($this->sql);
try
{
$num=mysql_num_rows($rs);
if(!$num)
{
throw new Exception("数据库中没有数据");
}
}
catch(Exception $e)
{
echo $e->getMessages();
return false;
}
return $num;
}
/*获得插入数据ID*/
public function getId($sql="")
{
$this->sql=$sql;
if(empty($this->sql)) return false;
$this->execute($this->sql);
return mysql_insert_id();
}
public function getClose()
{
mysql_close($this->conn);
}
}
?>