Lớp xử lý dữ liệu giúp đơn giản hóa trong việc lập trình truy xuất dữ liệu

Code:
<?php
class MySQL{
    var $dbhost;
    var $dbuser;
    var $dbpass;
    var $dbname;
    var $conn;
    function MySQL($dbhost,$dbuser,$dbpass,$dbname){
        $this->dbhost = $dbhost;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
        $this->dbname = $dbname;
        $this->conn      = NULL;
        $this->sql    = NULL;
    }

    function connect(){
        $this->conn =@mysql_pconnect($this->dbhost,$this->dbuser,$this->dbpass) or die('Cannot connect database!');
        @mysql_select_db($this->dbname) or die('Cannot connect to '.$this->dbname);
        return true;
    }
    
    
    function prepare($sql){
        $this->sql = $sql;
    }
    
    function execute(){
        if(!$this->sql)  $this->getError('M03',M03);
        if(!$this->conn) $this->connect();
        $this->result = @mysql_query($this->sql) or $this->getError('M00',mysql_error(),$this->sql);
        $this->sql = NULL;
        return true;
    }
    
    function query($sql){
        $this->prepare($sql);
        return $this->execute();
    }
    function fetch(){
        if($this->result)
         return  mysql_fetch_assoc($this->result);
        return false;
        }

    function fetchAll(){
        if($this->result){
            $obj = NULL;
            while($rs = $this->fetch(false)) $obj[] = $rs;
            @mysql_free_result($this->result);
            return $obj;
        }
        return false;
    }
    
    /*
    Get Error Code(Error Number)
    return: ErrorNo neu khong thu hien duoc cau truy van
            NULL: neu viec thuc hien thanh cong
    */
    function errorCode(){
        return mysql_errno();
    }
    /*
    Get Error Info
    return: Array info about mysql
            NULL: if not error
    */
    function errorInfo(){
        if($this->errorCode()) {
            $obj[0] = $this->errorCode();
            $obj[2] = mysql_error();
            return $obj;
        }
        return false;    
    }
    /*
     Returns the number of rows affected by the last SQL
    */
    function rowCount(){
        return @mysql_affected_rows();
    }
    /*
    Returns the ID of the last inserted row or sequence value
    */
    function lastInsertId(){
        return @mysql_insert_id();
    }
    
    function close() {
        if($this->conn) mysql_close($this->conn);
        unset($this->conn);
    }
    function getError($code,$msg,$tip){
       print "Code: $code<br />
       Message: $msg<br />
       Tip: $tip"
    }
            
/* End Class*/
}
//Cách dùng:

$db = new MySQL('localhost','user','password','db name');
$db->query("SELECT * FROM table WHERE dieukien"); // thực hiện query
$db->fetch(); // lấy record cuối cùng( thường dùng đối với các câu select chỉ có 1 record kết quả)
$db->fetchAll(); // lấy tất cả các record trong câu query
$db->lastInsertId(); //lấy id cuối cùng (đối với câu query insert)
$db->rowCount(); // lấy số record mà câu query thực hiện
$db->errorCode(); // mã lỗi của câu query
$db->close(); //đóng kết nối
?>
các bạn có thể tham khảo thêm tại đây:

[Xin lỗi, chỉ có thành viên mới thấy link. ]

View more random threads same category: