精英盒子 -> 程序设计 -> [光进程]数据库连接类、数据集类、数据库信息文件 [打印本页]

jybox 2011-08-02 22:57

[光进程]数据库连接类、数据集类、数据库信息文件

该帖是这个三个文件的源码,使用说明等LightPHP初步完成再说............
数据库信息文件
  1. ;<?php /*
    ;下面各项分别是 主机、数据库名、用户名、密码、字符集、整理类型

    [MySQL]
    host=127.0.0.1
    dbname=jynettest
    user=root
    pwd=123123
    charset=utf8
    collate=utf8_general_ci

    ;*/ ?>

数据库连接类
  1. <?php
    /*
    **                --数据库连接类        
    **
    **                LightPHP-A0.1[1]                by精英王子
    **                2011.8.2/2011.8.2
    */
    class MySQL
    {
            var $dbCfgs;
            var $conn;
            var $isCon = false;
            function __construct($dh,$dn,$du,$dp,$dc,$do)
            {
                    if(!$dh)
                    {
                            $this->dbSets=parse_ini_file("dbCfg.ini.php");
                    }
                    else
                    {
                            $this->dbCfgs["host"] = $dh;
                            $this->dbCfgs["dbname"] = $dn;
                            $this->dbCfgs["user"] = $du;
                            $this->dbCfgs["pwd"] = $dp;
                            $this->dbCfgs["charset"] = $dc;
                            $this->dbCfgs["collate"] = $do;
                    }
            }
            function open()
            {
                    if(!$this->isCon)
                    {
                            $this->conn = mysql_connect($this->dbSets["host"],$this->dbSets["user"],$this->dbSets["pwd"])
                                    or die("无法创建数据连接");
                            mysql_query("SET NAMES ".$this->dbSets["charset"]);
                            $this->isCon = true;
                    }
            }
            function close()
            {
                    //关闭数据库连接
                    if($this->isCon)
                    {
                            @mysql_close($this->conn);
                            $this->isCon = false;
                    }
            }
            function __destruct()
            {
                    $this->close();
            }
            function SQL($sql,$SQLRS=true)
            {
                    if(!$this->isCon)
                            $this->open();
                    $db_selected = mysql_select_db($this->dbSets["dbname"],$this->conn)
                            or die("打开数据库失败");
                    if($SQLRS)
                    {
                            $result = new SQLRs(mysql_query($sql, $this->conn));
                    }
                    else
                    {
                            $result = mysql_query($sql, $this->conn);
                    }
                    return $result;
            }
            function reStart()
            {
                    $this->close();
                    $this->open();
            }
            function ver()
            {
                    return mysql_get_server_info();
            }
            function debug()
            {
                    print_r($this);
            }
    }
    ?>

数据集类
  1. <?php
    /*
    **        --数据集类    
    **
    **        LightPHP-A0.1[1]        by精英王子
    **        2011.8.2/2011.8.2
    */
    class SQLRs
    {
        var $_rsID=NULL;
        var $_row=NULL;
        var $_seek=0;
        function __construct($res=NULL)
        {
            if($res)
            {
                $this->_rsID=$res;
                $this->_seek=0;
            }
        }
        function open($SQLs,$conn)
        {
            $this->_rsID=$conn->SQL($SQLs,false);
            $this->_seek=0;
        }
        function __destruct()
        {
            $this->close();
        }
        function close()
        {    
            if($this->isCon())
            {
                //释放资源
                @mysql_free_result($this->_rsID);
                $this->_rsID=NULL;
                $this->_row=NULL;
            }
        }
        function __get($name)
        {
            return $this->value($name);
        }
        function value($name)
        {
            return $this->_row[$name];
        }
        function seek($rows=0)
        {
            $this->_seek=$rows;
            return @mysql_data_seek($this->_rsID,$rows);
        }
        function isCon()
        {
            if($this->_rsID)
            {
                return true;
            }
            else
            {
                return flase;
            }
        }
        function read()
        {
            $this->next();
        }
        function next()
        {
            $r=mysql_fetch_array($this->_rsID);
            if($r)
            {
                $this->_row=$r;
                $this->_seek=$this->_seek+1;
            }
            else
            {
                return false;
            }
        }
        function getNum()
        {
            return @mysql_num_rows($this->_rsID);
        }
        function changeRows()
        {
            return @mysql_affected_rows();
        }
        function autoID()
        {
            return @mysql_insert_id();
        }
        function debug()
        {
            print_r($this);
        }
    }
    ?>



abreto 2011-08-03 13:53
没什么说的

jybox 2011-08-03 13:59
今天第一个回复




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.041302 second(s),query:5 Gzip enabled