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);
    }

}

?>      
      
      
      
      
      
      

上一篇文章: 数据库自定义异常 下一篇文章: test

关于PHP博客

PHP blogger

博客-致力于分享PHP技术的开发心得, 记录站长生活的点点滴滴,PHP博客采用PHP 面向对象编程+Smarty模板引擎+Mysql数据库 开发(全站纯静态化与伪静态化)
QQ:334192009
电邮:admin@PHPdesigner.org

本站搜索

本站最新文章

博客最近档案

友情链接