好長時間沒用的------php-----資料庫連接類__資料庫

來源:互聯網
上載者:User

< ? php
/*
     檔案名稱:DataBase.php
     作用:資料庫類
     作者:Mr.Bool
     建立時間:2007.10.22
     修改時間:2007.10.23
*/
    require_once ( "Config.inc.php" ) ; //系統設定檔

    /*
    $DBHOST="localhost"; //主機名稱
    $DBUSER="root"; // 資料庫使用者名稱
    $DBPWD=""; //密碼
    $DBNAME="test" ; //資料庫名
    */
    //定義資料庫類

    class DataBase
    {
        //定義屬性

        var $ mConnId ;         //串連標識        

        var $ mSqlString ; //待執行的SQL語句

        var $ mResultArray ;     //執行Select語句返回的結果數組        

        
        //__construct(),建構函式,建立資料庫的串連

        function __construct( $ pHost , $ pUser , $ pPwd , $ pDbName ) {             
            $ this - > mConnId= mysql_connect ( $ pHost , $ pUser , $ pPwd ) ; //建立串連

            mysql_select_db ( $ pDbName , $ this - > mConnId) ;     //選擇資料庫

            mysql_query ( "set names 'gbk'" ) ; //設定資料庫編碼為GBK

        }
        
        //__destruct:解構函式,中斷連線

        function __destruct( ) {
            mysql_close ( $ this - > mConnId) ; //此處還有問題......

        }
        
        //執行SQL語句

        function ExecuteSql( ) {
            mysql_query ( $ this - > mSqlString) ;
        }
        
        //查詢資料,傳回值為對象數組,數組中的每一元素為一行記錄構成的對象

        function Query( ) {
            $ i = 0;
            $ query_result = mysql_query ( $ this - > mSqlString, $ this - > mConnId) ;
            while ( $ row = mysql_fetch_object ( $ query_result ) ) {
                $ this - > mResultArray[ $ i + + ] = $ row ;
            }
        }
    
    } //class DataBase


    //以下為測試用

    $ db = new DataBase( $ DBHOST , $ DBUSER , $ DBPWD , $ DBNAME ) ;
    $ db - > mSqlString= "update student set phone='123' where id='04261001' " ;
    $ db - > ExecuteSql( ) ;
    $ db - > mSqlString= "select * from student where id='04261001' " ;
    $ db - > Query( ) ;
    print_r ( $ db - > mResultArray) ; //輸出測試結果

    
? >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.