Heim > php教程 > php手册 > 封装自己的DB类(PHP),封装db类php

封装自己的DB类(PHP),封装db类php

WBOY
Freigeben: 2016-06-13 08:45:35
Original
1214 Leute haben es durchsucht

封装自己的DB类(PHP),封装db类php

封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现。这样有了自己的DB类,写项目时简单的sql语句就不用每次写了,直接调用就行,很方便!

1.封装一个DB类。一个类文件应该只有一个类,其他的内容全部都没有。类文件的命名规则:类名.class.php

下面是创建DB类的代码:

<?<span>php

    </span><span>//</span><span>封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现</span>
    <span>class</span><span> DB{
        </span><span>//</span><span>属性</span>
        <span>private</span> <span>$host</span><span>;
        </span><span>private</span> <span>$port</span><span>;
        </span><span>private</span> <span>$user</span><span>;
        </span><span>private</span> <span>$pass</span><span>;
        </span><span>private</span> <span>$dbname</span><span>;
        </span><span>private</span> <span>$charset</span><span>;
        </span><span>private</span> <span>$prefix</span>;            <span>//</span><span>表前缀</span>
        <span>private</span> <span>$link</span>;                <span>//</span><span>连接资源(连接数据库,一般会返回一个资源,所以需要定义一个link属性)

        //构造方法(作用:为了初始化对象的属性),会被自动调用</span>
        <span>/*</span><span>
         * @param1 array $arr,默认为空,里面是一个关联数组,里面有7个元素
         * array('host' => 'localhost','port' => '3306');
         </span><span>*/</span>
        <span>public</span> <span>function</span> __construct(<span>$arr</span> = <span>array</span><span>()){
            </span><span>//</span><span>初始化</span>
            <span>$this</span>->host = <span>isset</span>(<span>$arr</span>['host']) ? <span>$arr</span>['host'] : 'localhost';<span>//</span><span>先判断是否有自己的host,如果有就用自己的host,否则就使用默认的localhost</span>
            <span>$this</span>->port = <span>isset</span>(<span>$arr</span>['port']) ? <span>$arr</span>['port'] : '3306'<span>;
            </span><span>$this</span>->user = <span>isset</span>(<span>$arr</span>['user']) ? <span>$arr</span>['user'] : 'root'<span>;
            </span><span>$this</span>->pass = <span>isset</span>(<span>$arr</span>['pass']) ? <span>$arr</span>['pass'] : 'root'<span>;
            </span><span>$this</span>->dbname = <span>isset</span>(<span>$arr</span>['dbname']) ? <span>$arr</span>['dbname'] : 'mydatabase'<span>;
            </span><span>$this</span>->charset = <span>isset</span>(<span>$arr</span>['charset']) ? <span>$arr</span>['charset'] : 'utf8'<span>;
            </span><span>$this</span>->prefix = <span>isset</span>(<span>$arr</span>['prefix']) ? <span>$arr</span>['prefix'] : ''<span>;

            </span><span>//</span><span>连接数据库(类是要操作数据库,因此要连接数据库)</span>
            <span>$this</span>-><span>connect();

            </span><span>//</span><span>设置字符集</span>
            <span>$this</span>-><span>setCharset();

            </span><span>//</span><span>选择数据库</span>
            <span>$this</span>-><span>setDbname();
        }

        </span><span>/*</span><span>
         * 连接数据库
        </span><span>*/</span>
        <span>private</span> <span>function</span><span> connect(){
            </span><span>//</span><span>mysql扩展连接</span>
            <span>$this</span>->link = <span>mysql_connect</span>(<span>$this</span>->host . ':' . <span>$this</span>->port,<span>$this</span>->user,<span>$this</span>-><span>pass);

            </span><span>//</span><span>判断结果</span>
            <span>if</span>(!<span>$this</span>-><span>link){
                </span><span>//</span><span>结果出错了
                //暴力处理,如果是真实线上项目(生产环境)必须写入到日志文件</span>
                <span>echo</span> '数据库连接错误:<br/>'<span>;
                </span><span>echo</span> '错误编号' . <span>mysql_errno</span>() . '<br/>'<span>;
                </span><span>echo</span> '错误内容' . <span>mysql_error</span>() . '<br/>'<span>;
                </span><span>exit</span><span>;
            }
        }

        </span><span>/*</span><span>
         * 设置字符集
        </span><span>*/</span>
        <span>private</span> <span>function</span><span> setCharset(){
            </span><span>//</span><span>设置</span>
            <span>$this</span>->db_query("set names {<span>$this</span>->charset}"<span>);
        }

        </span><span>/*</span><span>
         * 选择数据库
        </span><span>*/</span>
        <span>private</span> <span>function</span><span> setDbname(){
            </span><span>$this</span>->db_query("use {<span>$this</span>->dbname}"<span>);
        }

        </span><span>/*</span><span>
         * 增加数据
         * @param1 string $sql,要执行的插入语句
         * @return boolean,成功返回是自动增长的ID,失败返回FALSE
        </span><span>*/</span>
        <span>public</span> <span>function</span> db_insert(<span>$sql</span><span>){
            </span><span>//</span><span>发送数据</span>
            <span>$this</span>->db_query(<span>$sql</span><span>);
            
            </span><span>//</span><span>成功返回自增ID</span>
            <span>return</span> <span>mysql_affected_rows</span>() ? <span>mysql_insert_id</span>() : <span>FALSE</span><span>;
        }

        </span><span>/*</span><span>
         * 删除数据
         * @param1 string $sql,要执行的删除语句
         * @return Boolean,成功返回受影响的行数,失败返回FALSE
        </span><span>*/</span>
        <span>public</span> <span>function</span> db_delete(<span>$sql</span><span>){
            </span><span>//</span><span>发送SQL</span>
            <span>$this</span>->db_query(<span>$sql</span><span>);

            </span><span>//</span><span>判断结果</span>
            <span>return</span> <span>mysql_affected_rows</span>() ? <span>mysql_affected_rows</span>() : <span>FALSE</span><span>;
        }

        </span><span>/*</span><span>
         * 更新数据
         * @param1 string $sql,要执行的更新语句
         * @return Boolean,成功返回受影响的行数,失败返回FALSE
        </span><span>*/</span>
        <span>public</span> <span>function</span> db_update(<span>$sql</span><span>){
            </span><span>//</span><span>发送SQL</span>
            <span>$this</span>->db_query(<span>$sql</span><span>);

            </span><span>//</span><span>判断结果</span>
            <span>return</span> <span>mysql_affected_rows</span>() ? <span>mysql_affected_rows</span>() : <span>FALSE</span><span>;
        }

        </span><span>/*</span><span>
         * 查询:查询一条记录
         * @param1 string $sql,要查询的SQL语句
         * @return mixed,成功返回一个数组,失败返回FALSE
        </span><span>*/</span>
        <span>public</span> <span>function</span> db_getRow(<span>$sql</span><span>){
            </span><span>//</span><span>发送SQL</span>
            <span>$res</span> = <span>$this</span>->db_query(<span>$sql</span><span>);

            </span><span>//</span><span>判断返回</span>
            <span>return</span> <span>mysql_num_rows</span>(<span>$res</span>) ? <span>mysql_fetch_assoc</span>(<span>$res</span>) : <span>FALSE</span><span>;
        }

        </span><span>/*</span><span>
         * 查询:查询多条记录
         * @param1 string $sql,要查询的SQL语句
         * @return mixed,成功返回一个二维数组,失败返回FALSE
        </span><span>*/</span>
        <span>public</span> <span>function</span> db_getAll(<span>$sql</span><span>){
            </span><span>//</span><span>发送SQL</span>
            <span>$res</span> = <span>$this</span>->db_query(<span>$sql</span><span>);

            </span><span>//</span><span>判断返回</span>
            <span>if</span>(<span>mysql_num_rows</span>(<span>$res</span><span>)){
                </span><span>//</span><span>循环遍历</span>
                <span>$list</span> = <span>array</span><span>();
                
                </span><span>//</span><span>遍历</span>
                <span>while</span>(<span>$row</span> = <span>mysql_fetch_assoc</span>(<span>$res</span><span>)){
                    </span><span>$list</span>[] = <span>$row</span><span>;
                }

                </span><span>//</span><span>返回</span>
                <span>return</span> <span>$list</span><span>;
            }

            </span><span>//</span><span>返回FALSE</span>
            <span>return</span> <span>FALSE</span><span>;
        }

        </span><span>/*</span><span>
         * mysql_query错误处理
         * @param1 string $sql,需要执行的SQL语句
         * @return mixed,只要语句不出错,全部返回
        </span><span>*/</span>
        <span>private</span> <span>function</span> db_query(<span>$sql</span><span>){
            </span><span>//</span><span>发送SQL</span>
            <span>$res</span> = <span>mysql_query</span>(<span>$sql</span><span>);

            </span><span>//</span><span>判断结果</span>
            <span>if</span>(!<span>$res</span><span>){
                </span><span>//</span><span>结果出错了
                //暴力处理,如果是真实线上项目(生产环境)必须写入到日志文件</span>
                <span>echo</span> '语句出现错误:<br/>'<span>;
                </span><span>echo</span> '错误编号' . <span>mysql_errno</span>() . '<br/>'<span>;
                </span><span>echo</span> '错误内容' . <span>mysql_error</span>() . '<br/>'<span>;
                </span><span>exit</span><span>;
            }
            </span><span>//</span><span>没有错误</span>
            <span>return</span> <span>$res</span><span>;
        }
            </span><span>//</span><span>__sleep方法</span>
        <span>public</span> <span>function</span><span> __sleep(){
            </span><span>//</span><span>返回需要保存的属性的数组</span>
            <span>return</span> <span>array</span>('host','port','user','pass','dbname','charset','prefix'<span>);
        }

        </span><span>//</span><span>__wakeup方法</span>
        <span>public</span> <span>function</span><span> __wakeup(){
            </span><span>//</span><span>连接资源</span>
            <span>$this</span>-><span>connect();
            </span><span>//</span><span>设置字符集和选中数据库</span>
            <span>$this</span>-><span>setCharset();
            </span><span>$this</span>-><span>setDbname();
        }    

        </span><span>/*</span><span>
         * 获取完整的表名
        </span><span>*/</span>
        <span>protected</span> <span>function</span><span> getTableName(){
            </span><span>//</span><span>完整表名:前缀+表名</span>
            <span>return</span> <span>$this</span>->prefix . <span>$this</span>-><span>table;
        }
    }
</span><span>//</span><span>这个DB类,一般不写析构(不释放资源)    </span>
Nach dem Login kopieren

2.使用DB类,但是如果要使用某一个类来创建对象,必须要保证该类已经被加载到代码区里。可以使用一个魔术函数来实现对类的自动加载

自动加载:当一个脚本文件在执行某些语句的时候(实例化)需要去代码区里寻找对应的类,如果找不到会通过自动加载函数来将对应的类文件进行加载。

魔术函数__autoload()

比如我们在index.php页面中需要使用DB类,那么我们就直接调用即可,具体代码如下:

<?<span>php
    </span><span>//</span><span>使用DB类的对象来访问数据库
    //先加载类文件
    //include_once 'DB.class.php';
        //如果想使用其他类,又需要加载(所以使用魔术函数__autoload来实现类的自动加载)
    //显示的写出魔术函数__autoload
    //参数:需要加载的类的名字</span>
    <span>function</span> __autoload(<span>$a</span><span>){
        </span><span>//</span><span>将对应的类文件加载进来</span>
        <span>if</span>(<span>is_file</span>("<span>$a</span>.class.php"<span>)){
            </span><span>include_once</span> "<span>$a</span>.class.php"<span>;
        }
    }
    </span><span>//</span><span>实例化</span>
    <span>$db</span> = <span>new</span> DB(<span>array</span>('dbname' => 'mydatabase'<span>));

    
    </span>
Nach dem Login kopieren

 

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage