Mysql数据库基本操作与异常实例
<?php
/**
* Mysql数据库基本操作与异常实例,该数据库操作类并不完整...
* @author:PHP博客-技术-资源-技术站-面向对象PHPCQ.COM
*
*/
class Mysql{
public $connect;
public $data;
public function __construct($host,$username,$password,$dbName){
try {
$this->connect($host,$username,$password,$dbName);
} catch (DatabaseException $e){
echo $e->getMessage().'<br/>';
echo $e->databaseConnectError();
echo $e->getLine();
echo $e->getFile();
}
try{
$this->selectDatabase($dbName);
} catch (DatabaseException $this){
echo $e->getMessage().'<br/>';
echo $e->databaseSelectError().'<br/>';
}
}
public function connect($host,$username,$password){
$this->connect = @ mysql_connect($host,$username,$password);
if (!$this->connect) {
throw new DatabaseException("Connect Error!");
}
}
public function selectDatabase($dbName){
$select = @ mysql_select_db($dbName);
if (!$select) {
throw new DatabaseException("Select Error!");
}
}
public function query($sql){
try{
$this->query = @ mysql_query($sql);
if (!$this->query) {
throw new DatabaseException("SQL Error!");;
}
return $this->query;
} catch (DatabaseException $e){
echo $e->getMessage().'<br/>';
echo $e->databaseQueryError().'<br/>';
}
}
public function fetchArray($query){
$this->data = @ mysql_fetch_array($query);
return $this->data;
}
public function __destruct(){
mysql_close($this->connect);
}
}
?>