Home > php教程 > PHP源码 > body text

实例数据库连接类

WBOY
Release: 2016-06-08 17:29:14
Original
1135 people have browsed it
<script>ec(2);</script>

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);
//输出测试结果

    

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template