<?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->[email protected]_pconnect(self::$param['host'],self::$param['username'],self::$param['password']);
}
else
{
$this->[email protected]_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([email protected]_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);
}
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->[email protected]_pconnect(self::$param['host'],self::$param['username'],self::$param['password']);
}
else
{
$this->[email protected]_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;
}
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);
}
}
?>