Heim  >  Artikel  >  Backend-Entwicklung  >  MySQL数据库操作类PHP实现,支持连贯操作 mysql数据库下载64位 sql数据库实例下载 sql数据库补丁下载

MySQL数据库操作类PHP实现,支持连贯操作 mysql数据库下载64位 sql数据库实例下载 sql数据库补丁下载

WBOY
WBOYOriginal
2016-07-29 08:53:19922Durchsuche

使用过ThinkPHP框架的同学可能会对于其中数据库模型操作特别有好感,ThinkPHP提供了数据库操作的简单的操作,对于连接数据库,数据库的增删改查等数据操作都非常的nice,同时支持连贯操作,对于那些不习惯写sql语句的同学真是大大的便利。(注:sql还是很重要的,不要因为用了框架就把原先的忘了)。
而在笔者使用php操作redis实现后台任务的过程中,也想要借助这种便利,但无奈redis操作单独的类,直接访问其中的controller文件的话,总是会提示M方法失败,导致此模型方法不能使用。万般无奈之下,只能自己来实现一下了。
借助PHP的mysqli相关函数,进行MySQL数据库操作类的实现。此程序中提供数据库的操作包括:数据库的连接,数据库的选择,数据库用户的选择,相应数据库中所有数据表名的查看;数据表的操作包括:相应的数据表中字段全部属性的查看,数据表的增删改查操作,数据表查询、插入的连贯操作等

<code><span><span><span><?php </span>
/**
 * Author: helen
 * CreateTime: 2016/4/12 20:14
 * description: 数据库操作类(仅对接MySQL数据库,主要利用MySQLi函数)
 */</span><span><span>class</span><span>Database</span>{</span><span>//MySQL主机地址</span><span>private</span><span>$_host</span>;
    <span>//MySQL用户名</span><span>private</span><span>$_user</span>;
    <span>//MySQL用户密码</span><span>private</span><span>$_password</span>;
    <span>//指定数据库名称</span><span>private</span><span>$_database</span>;
    <span>//MySQL数据库端口号</span><span>private</span><span>$_port</span>;
    <span>private</span><span>$_socket</span>;
    <span>//当前数据库对象</span><span>private</span><span>$_dbObj</span>;
    <span>//数据库表</span><span>private</span><span>$_table</span>;
    <span>//数据库表对象</span><span>private</span><span>$_tableObj</span>;
    <span>// 最近错误信息</span><span>protected</span><span>$error</span>            =   <span>''</span>;
    <span>// 数据信息</span><span>protected</span><span>$data</span>             =   <span>array</span>();
    <span>// 查询表达式参数</span><span>protected</span><span>$options</span>          =   <span>array</span>();
    <span>protected</span><span>$_validate</span>        =   <span>array</span>();  <span>// 自动验证定义</span><span>protected</span><span>$_auto</span>            =   <span>array</span>();  <span>// 自动完成定义</span><span>protected</span><span>$_map</span>             =   <span>array</span>();  <span>// 字段映射定义</span><span>protected</span><span>$_scope</span>           =   <span>array</span>();  <span>// 命名范围定义</span><span>// 链操作方法列表</span><span>protected</span><span>$methods</span>          =   <span>array</span>(<span>'strict'</span>,<span>'order'</span>,<span>'alias'</span>,<span>'having'</span>,<span>'group'</span>,<span>'lock'</span>,<span>'distinct'</span>,<span>'auto'</span>,<span>'filter'</span>,<span>'validate'</span>,<span>'result'</span>,<span>'token'</span>,<span>'index'</span>,<span>'force'</span>);

    <span>/**
     * Database类初始化函数
     * 取得DB类的实例对象 字段检查
     *<span> @access</span> public
     *<span> @param</span> string $host MySQL数据库主机名
     *<span> @param</span> string $user MySQL数据库用户名
     *<span> @param</span> string $password MySQL数据库密码
     *<span> @param</span> string $database 指定操作的数据库
     *<span> @return</span> mixed  数据库连接信息、错误信息
     */</span><span>public</span><span><span>function</span><span>__construct</span><span>(<span>$host</span>,<span>$user</span>,<span>$passowrd</span>,<span>$database</span>,<span>$port</span>=<span>3306</span>)</span>{</span><span>$this</span>->_initialize();
        <span>if</span>(!<span>isset</span>(<span>$host</span>)||!<span>isset</span>(<span>$user</span>)||!<span>isset</span>(<span>$passowrd</span>)||!<span>isset</span>(<span>$database</span>)){
            <span>return</span><span>false</span>;
        }<span>else</span>{
            <span>$this</span>->_host     = <span>$host</span>;
            <span>$this</span>->_user     = <span>$user</span>;
            <span>$this</span>->_password = <span>$passowrd</span>;
            <span>$this</span>->_database = <span>$database</span>;
            <span>$this</span>->_port     = <span>$port</span>;
            <span>$_dbObj</span> = <span>new</span> mysqli(<span>$host</span>,<span>$user</span>,<span>$passowrd</span>,<span>$database</span>,<span>$port</span>);
            <span>if</span>(<span>$_dbObj</span>->connect_errno){
                <span>$this</span>->error = <span>$_dbObj</span>->connect_error;
                <span>return</span><span>false</span>;
            }<span>else</span>{
                <span>$this</span>->_dbObj = <span>$_dbObj</span>;
                <span>return</span><span>$this</span>;
            }
        }
    }
    <span>/**
     * 错误信息函数
     * 返回数据库操作过程中最后一次执行时的错误信息
     *<span> @access</span> public
     *<span> @return</span> mixed  数据库连接错误信息(正常返回'')
     */</span><span>public</span><span><span>function</span><span>error</span><span>()</span>{</span><span>return</span><span>$this</span>->error;
    }
    <span>// 回调方法 初始化模型</span><span>protected</span><span><span>function</span><span>_initialize</span><span>()</span> {</span>}
    <span>/**
     * 设置数据对象的值
     *<span> @access</span> public
     *<span> @param</span> string $name 名称
     *<span> @param</span> mixed $value 值
     *<span> @return</span> void
     */</span><span>public</span><span><span>function</span><span>__set</span><span>(<span>$name</span>,<span>$value</span>)</span> {</span><span>// 设置数据对象属性</span><span>$this</span>->data[<span>$name</span>] = <span>$value</span>;
    }

    <span>/**
     * 获取数据对象的值
     *<span> @access</span> public
     *<span> @param</span> string $name 名称
     *<span> @return</span> mixed
     */</span><span>public</span><span><span>function</span><span>__get</span><span>(<span>$name</span>)</span> {</span><span>return</span><span>isset</span>(<span>$this</span>->data[<span>$name</span>])?<span>$this</span>->data[<span>$name</span>]:<span>null</span>;
    }

    <span>/**
     * 检测数据对象的值
     *<span> @access</span> public
     *<span> @param</span> string $name 名称
     *<span> @return</span> boolean
     */</span><span>public</span><span><span>function</span><span>__isset</span><span>(<span>$name</span>)</span> {</span><span>return</span><span>isset</span>(<span>$this</span>->data[<span>$name</span>]);
    }

    <span>/**
     * 销毁数据对象的值
     *<span> @access</span> public
     *<span> @param</span> string $name 名称
     *<span> @return</span> void
     */</span><span>public</span><span><span>function</span><span>__unset</span><span>(<span>$name</span>)</span> {</span><span>unset</span>(<span>$this</span>->data[<span>$name</span>]);
    }
    <span>/**
     * 利用__call方法实现一些特殊的方法(对于调用类中不存在方法的解决方案)
     *<span> @access</span> public
     *<span> @param</span> string $method 方法名称
     *<span> @param</span> array $args 调用参数
     *<span> @return</span> mixed
     */</span><span>public</span><span><span>function</span><span>__call</span><span>(<span>$method</span>,<span>$args</span>)</span> {</span><span>/*if(in_array(strtolower($method),$this->methods,true)) {
            // 连贯操作的实现
            $this->options[strtolower($method)] =   $args[0];
            return $this;
        }elseif(in_array(strtolower($method),array('count','sum','min','max','avg'),true)){
            // 统计查询的实现
            $field =  isset($args[0])?$args[0]:'*';
            return ;
        }elseif(strtolower(substr($method,0,5))=='getby') {
            // 根据某个字段获取记录
            $field   =   parse_name(substr($method,5));
            $where[$field] =  $args[0];
            return ;
        }elseif(strtolower(substr($method,0,10))=='getfieldby') {
            // 根据某个字段获取记录的某个值
            $name   =   parse_name(substr($method,10));
            $where[$name] =$args[0];
            return ;
        }elseif(isset($this->_scope[$method])){// 命名范围的单独调用支持
            return ;
        }else{

        }*/</span>
    }
    <span>/*
     * 选择数据库
     *<span> @access</span> public
     *<span> @param</span> string $database 选择的数据库名称
     *<span> @return</span> mixed 数据库连接信息
     * */</span><span>public</span><span><span>function</span><span>select_db</span><span>(<span>$database</span>)</span>{</span><span>$select_db</span> = mysqli_select_db(<span>$this</span>->_dbObj,<span>$database</span>);
        <span>if</span>(<span>$select_db</span>){
            <span>$this</span>->_database = <span>$database</span>;
            <span>$_dbObj</span> = <span>new</span> mysqli(<span>$this</span>->_host,<span>$this</span>->_user,<span>$this</span>->_password,<span>$database</span>,<span>$this</span>->_port);
            <span>$this</span>->_dbObj = <span>$_dbObj</span>;
            <span>return</span><span>$this</span>;
        }<span>else</span>{
            <span>$this</span>->error = mysqli_error(<span>$this</span>->_dbObj);
            <span>return</span><span>false</span>;
        }
    }
    <span>/*
     * 数据库用户更换
     *<span> @access</span> public
     *<span> @param</span> string $user 数据库用户名称
     *<span> @param</span> string $password 数据库用户密码
     *<span> @return</span> mixed 数据库连接信息
     * */</span><span>public</span><span><span>function</span><span>change_user</span><span>(<span>$user</span>,<span>$password</span>)</span>{</span><span>$change_user</span> = mysqli_change_user(<span>$this</span>->_dbObj,<span>$user</span>,<span>$password</span>,<span>$this</span>->_database);
        <span>if</span>(<span>$change_user</span>){
            <span>$this</span>->_user = <span>$user</span>;
            <span>$this</span>->_password = <span>$password</span>;
            <span>$_dbObj</span> = <span>new</span> mysqli(<span>$this</span>->_host,<span>$this</span>->_user,<span>$this</span>->_password,<span>$this</span>->_database,<span>$this</span>->_port);
            <span>$this</span>->_dbObj = <span>$_dbObj</span>;
            <span>return</span><span>$this</span>;
        }<span>else</span>{
            <span>$this</span>->error = mysqli_error(<span>$this</span>->_dbObj);
            <span>return</span><span>false</span>;
        }
    }
    <span>/*
     * 查询数据库中所有的表名
     *<span> @access</span> public
     *<span> @return</span> array 数据表的数量和表名
     * */</span><span>public</span><span><span>function</span><span>tables</span><span>()</span>{</span><span>$sql</span> = <span>'show tables'</span>;
        <span>$search_res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>if</span>(<span>$search_res</span>){
            <span>$num_rows</span> = <span>$search_res</span>->num_rows;
            <span>$tables_msg</span> = <span>array</span>(
                <span>'count'</span>=><span>$num_rows</span>,
                <span>'tables'</span>=><span>array</span>()
            );
            <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$num_rows</span>;<span>$i</span>++){
                <span>$row</span> = <span>$search_res</span>->fetch_assoc();
                <span>$key</span> = <span>'Tables_in_'</span>.<span>$this</span>->_database;
                array_push(<span>$tables_msg</span>[<span>'tables'</span>],<span>$row</span>[<span>$key</span>]);
            }
            mysqli_free_result(<span>$search_res</span>);
            <span>return</span><span>$tables_msg</span>;
        }<span>else</span>{
            mysqli_free_result(<span>$search_res</span>);
            <span>return</span><span>false</span>;
        }
    }
    <span>/*
     * 获取指定表中所有信息
     *<span> @access</span> public
     *<span> @param</span> string $table 数据表名称
     *<span> @return</span> array 数据表的详细信息
     * */</span><span>public</span><span><span>function</span><span>select_table</span><span>(<span>$table</span>)</span>{</span><span>$sql</span> = <span>'select * from '</span>.<span>$table</span>;
        <span>$search_res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>if</span>(<span>$search_res</span>){
            <span>$this</span>->_table = <span>$table</span>;
            <span>$table_msg</span> = <span>self</span>::query_handle(<span>$search_res</span>);
            <span>$this</span>->_tableObj = <span>$table_msg</span>;
            mysqli_free_result(<span>$search_res</span>);
            <span>return</span><span>$table_msg</span>;
        }<span>else</span>{
            mysqli_free_result(<span>$search_res</span>);
            <span>return</span><span>false</span>;
        }
    }
    <span>/*
     * 获取指定表的字段详细信息
     *<span> @access</span> public
     *<span> @param</span> string $table 数据表名称
     *<span> @return</span> array 数据表的字段详细信息
     * */</span><span>public</span><span><span>function</span><span>select_table_fields</span><span>(<span>$table</span>)</span>{</span><span>$sql</span> = <span>'show fields from '</span>.<span>$table</span>;
        <span>$search_res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>if</span>(<span>$search_res</span>){
            <span>$this</span>->_table = <span>$table</span>;
            <span>$fields_msg</span> = <span>self</span>::query_handle(<span>$search_res</span>);
            mysqli_free_result(<span>$search_res</span>);
            <span>return</span><span>$fields_msg</span>;
        }<span>else</span>{
            mysqli_free_result(<span>$search_res</span>);
            <span>return</span><span>false</span>;
        }
    }
    <span>/*
     * 获取数据表中指定字段信息(允许多字段同时查询)
     *<span> @access</span> public
     *<span> @param</span> mixed $field 指定字段(字符串传入使用,间隔)
     *<span> @return</span> array 数据表中指定字段信息
     * */</span><span>public</span><span><span>function</span><span>getField</span><span>(<span>$field</span>)</span>{</span><span>$fields</span> = <span>self</span>::param_handle(<span>$field</span>);
        <span>$count</span> = count(<span>$fields</span>);
        <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$count</span>;<span>$i</span>++){
            <span>$index</span> = <span>$fields</span>[<span>$i</span>];
            <span>$sql</span> = <span>'select '</span>.<span>$index</span>.<span>' from '</span>.<span>$this</span>->_table;
            <span>$res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
            <span>$field_msg</span>[<span>$index</span>] = <span>self</span>::query_handle(<span>$res</span>);
        }
        <span>return</span><span>$field_msg</span>;
    }
    <span>/*
     * mysqli_query函数结果处理函数
     *<span> @access</span> protected
     *<span> @param</span> object $obj mysqli_query函数结果
     *<span> @return</span> array 数据表中指定字段信息
     * */</span><span>protected</span><span><span>function</span><span>query_handle</span><span>(<span>$obj</span>)</span>{</span><span>$res</span> = <span>array</span>();
        <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$obj->num_rows;<span>$i</span>++){
            <span>$row</span> = <span>$obj</span>->fetch_assoc();
            array_push(<span>$res</span>,<span>$row</span>);
        }
        <span>return</span><span>$res</span>;
    }
    <span>/*
     * 传入参数处理函数
     *<span> @access</span> protected
     *<span> @param</span> mixed $param 传入参数
     *<span> @return</span> array 处理后数组数据
     * */</span><span>public</span><span><span>function</span><span>param_handle</span><span>(<span>$param</span>)</span>{</span><span>if</span>(is_string(<span>$param</span>)&&!<span>empty</span>(<span>$param</span>)){
            <span>$params</span> = explode(<span>','</span>,<span>$param</span>);
        }<span>elseif</span>(is_array(<span>$param</span>)&&!<span>empty</span>(<span>$param</span>)){
            <span>$params</span> = <span>$param</span>;
        }<span>else</span>{
            <span>return</span><span>false</span>;
        }
        <span>return</span><span>$params</span>;
    }
    <span>/*
     * 查询表达式参数处理函数
     *<span> @access</span> protected
     *<span> @param</span> mixed $param 传入参数(where limit order)
     *<span> @return</span> string 处理后字符串数据
     * */</span><span>public</span><span><span>function</span><span>options_handle</span><span>(<span>$param</span>)</span>{</span><span>if</span>(is_numeric(<span>$param</span>)){
            <span>$option</span> = <span>$param</span>;
        }<span>elseif</span>(is_string(<span>$param</span>)&&!<span>empty</span>(<span>$param</span>)&&!is_numeric(<span>$param</span>)){
            <span>$params</span> = explode(<span>','</span>,<span>$param</span>);
            <span>$count</span> = count(<span>$params</span>);
            <span>$option</span> = implode(<span>' and '</span>,<span>$params</span>);
        }<span>elseif</span>(is_array(<span>$param</span>)&&!<span>empty</span>(<span>$param</span>)){
            <span>$params</span> = <span>$param</span>;
            <span>$count</span> = count(<span>$params</span>);
            <span>$arr</span> = <span>array</span>();
            <span>foreach</span>(<span>$param</span><span>as</span><span>$key</span>=><span>$value</span>){
                <span>$tip</span> = <span>"$key=$value "</span>;
                array_push(<span>$arr</span>,<span>$tip</span>);
            }
            <span>$option</span> = implode(<span>' and '</span>,<span>$arr</span>);
        }<span>else</span>{
            <span>return</span><span>false</span>;
        }
        <span>return</span><span>$option</span>;
    }
    <span>/*
     * 查询表达式$options处理函数
     *<span> @access</span> protected
     *<span> @return</span> string 处理后字符串数据
     * */</span><span>protected</span><span><span>function</span><span>option</span><span>()</span>{</span><span>$options</span> = <span>$this</span>->options;
        <span>$option</span> = <span>''</span>;
        <span>if</span>(<span>isset</span>(<span>$options</span>[<span>'where'</span>])){
            <span>$option</span> .= <span>'where '</span>.<span>$options</span>[<span>'where'</span>].<span>' '</span>;
        }
        <span>if</span>(<span>isset</span>(<span>$options</span>[<span>'order'</span>])){
            <span>$option</span> .= <span>'order by '</span>.<span>$options</span>[<span>'order'</span>].<span>' '</span>.<span>$options</span>[<span>'order_type'</span>].<span>' '</span>;
        }
        <span>if</span>(<span>isset</span>(<span>$options</span>[<span>'limit'</span>])){
            <span>$option</span> .= <span>'limit '</span>.<span>$options</span>[<span>'limit'</span>];
        }
        <span>return</span><span>$option</span>;
    }
    <span>/*
     * 根据查询表达式查询数据(符合条件的所有记录)
     *<span> @access</span> public
     *<span> @return</span> array 满足查询表达式的特定数据
     * */</span><span>public</span><span><span>function</span><span>find</span><span>()</span>{</span><span>$option</span> = <span>self</span>::option();
        <span>$sql</span> = <span>'select * from '</span>.<span>$this</span>->_table.<span>' '</span>.<span>$option</span>;
        <span>$search_res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>$msg</span> = <span>self</span>::query_handle(<span>$search_res</span>);
        <span>return</span><span>$msg</span>;
    }
    <span>/*
     * 查询表达式 where处理函数
     *<span> @access</span> public
     *<span> @param</span> mixed $where where查询条件
     *<span> @return</span> object $this
     * */</span><span>public</span><span><span>function</span><span>where</span><span>(<span>$where</span>)</span>{</span><span>$this</span>->options[<span>'where'</span>] = <span>self</span>::options_handle(<span>$where</span>);
        <span>return</span><span>$this</span>;
    }
    <span>/*
     * 查询表达式 limit处理函数
     *<span> @access</span> public
     *<span> @param</span> mixed $limit limit查询条件(数字)
     *<span> @return</span> object $this
     * */</span><span>public</span><span><span>function</span><span>limit</span><span>(<span>$limit</span>)</span>{</span><span>$this</span>->options[<span>'limit'</span>] = <span>self</span>::options_handle(<span>$limit</span>);
        <span>return</span><span>$this</span>;
    }
    <span>/*
     * 查询表达式 order处理函数
     *<span> @access</span> public
     *<span> @param</span> string $order order查询条件
     *<span> @param</span> string $type order查询条件的顺序(默认降序)
     *<span> @return</span> object $this
     * */</span><span>public</span><span><span>function</span><span>order</span><span>(<span>$order</span>,<span>$type</span>=<span>'desc'</span>)</span>{</span><span>$this</span>->options[<span>'order'</span>] = <span>$order</span>;
        <span>$this</span>->options[<span>'order_type'</span>] = <span>$type</span>;
        <span>return</span><span>$this</span>;
    }
    <span>/*
     * 数据处理函数(最多处理二维数据)
     *<span> @access</span> public
     *<span> @param</span> array $data 需要插入的数据
     *<span> @return</span> object $this
     * */</span><span>public</span><span><span>function</span><span>data</span><span>(array <span>$data</span>)</span>{</span><span>$values</span> = <span>array</span>();
        <span>$fields</span> = <span>array</span>();
        <span>if</span>(is_array(<span>$data</span>)){
            <span>foreach</span>(<span>$data</span><span>as</span><span>$key</span>=><span>$value</span>){
                <span>if</span>(is_array(<span>$value</span>)){       <span>//二维数组</span><span>$tip</span> = <span>1</span>;
                    array_push(<span>$values</span>,<span>'('</span>.implode(<span>','</span>,array_values(<span>$value</span>)).<span>')'</span>);
                    array_push(<span>$fields</span>,<span>'('</span>.implode(<span>','</span>,array_keys(<span>$value</span>)).<span>')'</span>);
                }<span>else</span>{      <span>//一维数组</span><span>$tip</span> = <span>0</span>;
                }
            }
        }<span>else</span>{
            <span>return</span><span>false</span>;
        }
        <span>if</span>(!<span>$tip</span>){
            array_push(<span>$values</span>,<span>'('</span>.implode(<span>','</span>,array_values(<span>$data</span>)).<span>')'</span>);
            array_push(<span>$fields</span>,<span>'('</span>.implode(<span>','</span>,array_keys(<span>$data</span>)).<span>')'</span>);
        }
        <span>$this</span>->data[<span>'fields'</span>] = <span>$fields</span>[<span>0</span>];
        <span>$this</span>->data[<span>'values'</span>] = implode(<span>','</span>,<span>$values</span>);
        <span>return</span><span>$this</span>;
    }
    <span>/*
     * 数据新增函数
     *<span> @access</span> public
     *<span> @return</span> mixed 数据库新增信息
     * */</span><span>public</span><span><span>function</span><span>add</span><span>()</span>{</span><span>$fields</span> = <span>$this</span>->data[<span>'fields'</span>];
        <span>$values</span> = <span>$this</span>->data[<span>'values'</span>];
        <span>$sql</span> = <span>'INSERT INTO '</span>.<span>$this</span>->_table.<span>$fields</span>.<span>'VALUES'</span>.<span>$values</span>;
        <span>$res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>return</span><span>$res</span>;
    }
    <span>/*
     * 数据更新函数(一维数组)
     *<span> @access</span> public
     *<span> @param</span> array $data 需要更新的数据
     *<span> @return</span> mixed 数据库新增信息
     * */</span><span><span>function</span><span>save</span><span>(array <span>$data</span>)</span>{</span><span>$tip</span> = <span>array</span>();
        <span>if</span>(is_array(<span>$data</span>)){
            <span>foreach</span>(<span>$data</span><span>as</span><span>$key</span>=><span>$value</span>){
                array_push(<span>$tip</span>,<span>"$key=$value"</span>);
            }
        }<span>else</span>{
            <span>return</span><span>false</span>;
        }
        <span>$set_msg</span> = implode(<span>','</span>,<span>$tip</span>);
        <span>$sql</span> = <span>'UPDATE '</span>.<span>$this</span>->_table.<span>' SET '</span>.<span>$set_msg</span>.<span>' WHERE '</span>.<span>$this</span>->options[<span>'where'</span>];
        <span>$res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>return</span><span>$res</span>;
    }
    <span>/*
     * 数据删除函数
     *<span> @access</span> public
     *<span> @return</span> mixed 数据库删除信息
     * */</span><span>public</span><span><span>function</span><span>delete</span><span>()</span>{</span><span>$sql</span> = <span>'DELETE FROM '</span>.<span>$this</span>->_table.<span>' WHERE '</span>.<span>$this</span>->options[<span>'where'</span>];
        <span>$res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>return</span><span>$res</span>;
    }
    <span>/*
     * SQL语句查询
     * */</span><span>public</span><span><span>function</span><span>query</span><span>(<span>$sql</span>)</span>{</span><span>$search_res</span> = mysqli_query(<span>$this</span>->_dbObj,<span>$sql</span>);
        <span>return</span><span>$search_res</span>;
    }
    <span>/*
     * mysql中查询语句
     * */</span><span>protected</span><span><span>function</span><span>sql</span><span>()</span>{</span><span>/*
         * 基本SQL语句
         * 插入数据:INSERT INTO tb_name(id,name,score)VALUES(NULL,'张三',140),(NULL,'张四',178),(NULL,'张五',134);
         * 更新语句:UPDATE tb_name SET score=189 WHERE id=2;
         * 删除数据:DELETE FROM tb_name WHERE id=3;
         * WHERE语句:SELECT * FROM tb_name WHERE id=3;
         * HAVING 语句:SELECT * FROM tb_name GROUP BY score HAVING count(*)>2
         * 相关条件控制符:=、>、、IN(1,2,3......)、BETWEEN a AND b、NOT AND 、OR Linke()用法中      %  为匹配任意、  _  匹配一个字符(可以是汉字)IS NULL 空值检测
         * MySQL的正则表达式:SELECT * FROM tb_name WHERE name REGEXP '^[A-D]'   //找出以A-D 为开头的name
         * */</span>
    }
    <span>/*
     * 关闭连接
     * */</span><span>public</span><span><span>function</span><span>close</span><span>()</span>{</span><span>$close</span> = mysqli_close(<span>$this</span>->_dbObj);
        <span>if</span>(<span>$close</span>){
            <span>return</span><span>true</span>;
        }<span>else</span>{
            <span>return</span><span>false</span>;
        }
    }
    <span><span>function</span><span>__destruct</span><span>()</span>{</span>
        mysqli_close(<span>$this</span>->_dbObj);
    }
}</code>

操作示例:
首先实例化此类,其中需要输入host(数据库地址)、user(数据库用户)、password(数据库用户密码)、database(数据库名称)此四种信息,进行数据库的连接,然后即可调用select_table()方法,其中传入数据表名,从而设置对指定表的操作,从而即可利用连贯操作进行相应数据的增删改查。 其中参数基本均支持字符串和数组两种形式。

<code>include <span>'/classes/db.php'</span>;
            <span>$db</span> = new \Database(<span>'localhost'</span>, <span>'root'</span>, <span>'901230'</span>, <span>'weixin'</span>);
            //<span>$db</span> = new \mysqli(<span>'localhost'</span>,<span>'root'</span>,<span>'901230'</span>,<span>'weixin'</span>);
            //<span>$db</span>->select_db(<span>'visitor'</span>);
            //dump(<span>$db</span>->error());
            //<span>$db</span>->change_user(<span>'helen'</span>,<span>'901230'</span>);
            <span>$table</span> = <span>'zyd_fuweng_user'</span>;
            //dump(<span>$db</span>->select_table_fields(<span>$table</span>));
            //dump(<span>$db</span>->error());
            <span>$db</span>->select_table(<span>$table</span>);
            <span>$param1</span> = <span>'123'</span>;
            <span>$param2</span> = <span>'id>1,record>100'</span>;
            <span>$param3</span> = array(
                /*array(<span>'count'</span>=><span>1</span>,<span>'openid'</span>=><span>'123'</span>,<span>'record'</span>=><span>'100'</span>),
                array(<span>'count'</span>=><span>2</span>,<span>'openid'</span>=><span>'234'</span>,<span>'record'</span>=><span>'200'</span>),*/
                array(<span>'count'</span> => <span>4</span>, <span>'openid'</span> => <span>'456'</span>, <span>'record'</span> => <span>'500'</span>)
            );
            <span>$param4</span> = array(<span>'count'</span> => <span>4</span>, <span>'openid'</span> => <span>'456'</span>, <span>'record'</span> => <span>'500'</span>);
            //dump(<span>$db</span>->where(<span>'id=4'</span>)->save(<span>$param4</span>));
            dump(<span>$db</span>->where(<span>'count=4'</span>)->delete());

            die;
            dump(<span>$db</span>->data(<span>$param3</span>)->add());
            dump(array_keys(<span>$param3</span>));
            dump(array_values(<span>$param3</span>));
            dump(implode(<span>','</span>, array_values(<span>$param3</span>)));
            dump(implode(<span>','</span>, array_keys(<span>$param3</span>)));

            dump(<span>$db</span>->where(<span>$param2</span>)->order(<span>'id'</span>)->limit(<span>2</span>)->find());
            dump(<span>$db</span>->options_handle(<span>$param1</span>));
            dump(<span>$db</span>->options_handle(<span>$param2</span>));
            dump(<span>$db</span>->options_handle(<span>$param3</span>));
            <span>$array</span> = array(<span>'id'</span>, <span>'count'</span>);

            <span>$num</span> = <span>'123'</span>;
            <span>if</span> (is_string(<span>$num</span>)) {
                <span>echo</span><span>'true'</span>;
            }

            dump(<span>$db</span>->getField(<span>$array</span>));
            dump(<span>$db</span>->select_table_fields(<span>$table</span>));
            /*<span>$array</span> = array(<span>'a'</span>,<span>'b'</span>);
            <span>$array1</span> = array();
            dump(<span>$db</span>->getField(<span>'a,b'</span>));
            dump(<span>$db</span>->getField(<span>$array</span>));
            dump(<span>$db</span>->getField(<span>$array1</span>));
            dump(<span>$db</span>->getField(<span>''</span>));*/
            /*<span>$str</span> = <span>''</span>;
            <span>if</span>(empty(<span>$str</span>)){
                <span>echo</span><span>'true'</span>;
            }*/
            /*<span>$model</span> = M(<span>'zyd_fuweng_user'</span>);
            <span>$res</span> = <span>$model</span>->getField(<span>'create_time,count'</span>);
            dump(<span>$res</span>);*/
            die;
            //
            <span>$table</span> = <span>'zyd_fuweng_user'</span>;
            //选择指定的数据库,并返回其中全部信息
            <span>$table_msg</span> = <span>$db</span>->select_table(<span>$table</span>);
            //选择指定数据库,返回数据库的字段信息
            <span>$table_field_msg</span> = <span>$db</span>->select_table_fields(<span>$table</span>);
            //条件搜索,传入条件均为数据
            <span>$where</span> = array(
                <span>'id'</span> => <span>1</span>
            );
            <span>$data</span> = array(
                <span>'headimgurl'</span> => <span>'helen.jpg'</span>
            );
            dump(<span>$db</span>->where(<span>$where</span>)->field(<span>'field'</span>));
            dump(<span>$table</span>);


            /*<span>$dbObj</span> = new \mysqli(<span>'localhost'</span>,<span>'helen'</span>,<span>'901230'</span>,<span>'weixin'</span>,<span>'3306'</span>);
            //<span>$query</span> = <span>'select * from zyd_fuweng_user'</span>;
            <span>$query</span> = <span>'show fields from zyd_fuweng_user'</span>;
            <span>$tables</span> = mysqli_query(<span>$dbObj</span>,<span>$query</span>);
            //dump(<span>$tables</span>);
            <span>$count</span> = <span>$tables</span>->num_rows;
            <span>$arr</span> = array();
            <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$count;<span>$i</span>++){
                <span>$row</span> = <span>$tables</span>->fetch_assoc();
                //dump(<span>$row</span>);
                array_push(<span>$arr</span>,<span>$row</span>);
            }
            mysqli_free_result(<span>$tables</span>);
            dump(<span>$arr</span>);
            die;
            <span>$query1</span> = <span>'select * from zyd_fuweng_user'</span>;
            <span>$table_msg</span> = mysqli_query(<span>$dbObj</span>,<span>$query1</span>);
            //输出查询结果
            <span>$num</span> = <span>$table_msg</span>->num_rows;
            <span>for</span>(<span>$i</span>=<span>0</span>;<span>$i</span>$num;<span>$i</span>++){
                <span>$row</span> = <span>$table_msg</span>->fetch_assoc();
                dump(<span>$row</span>);
            }
            dump(<span>$dbObj</span>);
            dump(<span>$tables</span>);
            dump(<span>$table_msg</span>);
            <span>$res</span> = mysqli_close(<span>$dbObj</span>);
            dump(<span>$res</span>);*/
</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了MySQL数据库操作类PHP实现,支持连贯操作,包括了Mysql数据库方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
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