Go Back   VN-Zoom forum > Chia sẻ kiến thức > Thiết kế web

 

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 03-06-2008, 12:03   #1
Gà Con
 
user100's Avatar
 
Tham gia: Jun 2008
Bài: 3
VZD: 224
Điểm: 0/0 bài viết
Smile code database cho php, rất đơn giản

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:



user100 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời

Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt
Trackbacks are Tắt
Pingbacks are Tắt
Refbacks are Tắt
Chuyển đến


Múi giờ GMT +7. Hiện tại là 12:57.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
Từ điển được cung cấp bởi VDict.com - Hosting được tài trợ bởi Rao vặt 123