热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MySQL操作类-PHP源码

MySQL操作类
';
//获取表字段
//print_r($mysql->getFields('test'));
//增
echo $mysql->data(array('name'=>'test','password'=>'123456'))->table('test')->add();
//删
echo $mysql->table('test')->where('id=1')->delete();
//改
echo $mysql->table(&#39;test&#39;)->data(array(&#39;name&#39;=>&#39;bbbbbbbbbbbb&#39;))->where(&#39;id<3&#39;)->update();
//查
print_r($mysql->table(&#39;test&#39;)->where(&#39;id=4&#39;)->select());
print_r($mysql->table(&#39;test&#39;)->order(&#39;id desc&#39;)->select());
//
$mysql->query(&#39;select * from `test`&#39;);
$mysql->execute(&#39;update `test` set password = 123&#39;);
echo &#39;
&#39;; echo &#39;查询次数:&#39;.$mysql->query_count.&#39;
&#39;; echo &#39;查询时间:&#39;.number_format(microtime(true)-($mysql->query_start_time),10).&#39; 秒
&#39;; echo &#39;错误信息:&#39;.$mysql->error().&#39;
&#39;; ?>

2. mysql.class.php

db_mysql_hostname = $hostname_or_conf[&#39;hostname&#39;];
            $this->db_mysql_username = $hostname_or_conf[&#39;username&#39;];
            $this->db_mysql_password = $hostname_or_conf[&#39;password&#39;];
            $this->db_mysql_database = $hostname_or_conf[&#39;database&#39;];
            $this->db_mysql_port = isset($hostname_or_conf[&#39;port&#39;])?$hostname_or_conf[&#39;port&#39;]:&#39;3306&#39;;
            $this->db_mysql_charset = isset($hostname_or_conf[&#39;charset&#39;])?$hostname_or_conf[&#39;charset&#39;]:&#39;utf8&#39;;
            
        }elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database))
        {
             $this->db_mysql_hostname = $hostname_or_conf;
             $this->db_mysql_username = $username;
             $this->db_mysql_password = $password;
             $this->db_mysql_database = $database;
             $this->db_mysql_port = $port;
             $this->db_mysql_charset = $char;
             
        }else{
            die(&#39;configuration error.&#39;);
        }
        $this->connect();
    }
    
    private function connect(){
        $server = $this->db_mysql_hostname.&#39;:&#39;.$this->db_mysql_port;
        $this->cOnn= mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true) or die(&#39;Connect MySQL DB error!&#39;);
        mysql_select_db($this->db_mysql_database,$this->conn) or die(&#39;select db error!&#39;);
        mysql_query("set names " . $this->db_mysql_charset, $this->conn);
    }
    /**
     +----------------------------------------------------------
     * 设置数据对象值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     *table,where,order,limit,data,field,join,group,having
     +----------------------------------------------------------
     */
    public function table($table){
        $this->query_list[&#39;table&#39;] = $table;
        return $this;
    }
    
    public function where($where){
        $this->query_list[&#39;where&#39;] = $where;
        return $this;
    }
    
    public function order($order){
        $this->query_list[&#39;order&#39;] = $order;
        return $this;
    }
    
    public function limit($offset,$length){
        if(!isset($length)){
            $length = $offset;
            $offset = 0;
        }
        $this->query_list[&#39;limit&#39;] = &#39;limit &#39;.$offset.&#39;,&#39;.$length;
        return $this;
    }
    
    public function data($data){
        /*
        if(is_object($data)){
            $data   =   get_object_vars($data);
        }elseif (is_string($data)){
            parse_str($data,$data);
        }elseif(!is_array($data)){
            //Log:DATA_TYPE_INVALID
        }
        */
        $this->query_list[&#39;data&#39;] = $data;
        return $this;
    }
    public function field($fields){
        $this->query_list[&#39;fields&#39;] = $fields;
        return $this;
    }
    public function join($join){
        $this->query_list[&#39;join&#39;] = $join;
        return $this;
    }
    public function group($group){
        $this->query_list[&#39;group&#39;] = $group;
        return $this;
    }
    public function having($having){
        $this->query_list[&#39;having&#39;] = $having;
        return $this;
    }
    /**
     +----------------------------------------------------------
     * 查询
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
    public function select(){
        $select_sql = &#39;select &#39;;
        $fields = isset($this->query_list[&#39;fields&#39;])?$this->query_list[&#39;fields&#39;]:&#39;*&#39;;
        $select_sql.=$fields;
        $select_sql.= &#39; from `&#39;.$this->query_list[&#39;table&#39;].&#39;` &#39;;
        
        isset($this->query_list[&#39;join&#39;])?($select_sql.=$this->query_list[&#39;join&#39;]):&#39;&#39;;
        isset($this->query_list[&#39;where&#39;])?($select_sql.=&#39; where &#39;.$this->query_list[&#39;where&#39;]):&#39;&#39;;
        isset($this->query_list[&#39;group&#39;])?($select_sql.=&#39; group by&#39;.$this->query_list[&#39;group&#39;]):&#39;&#39;;
        isset($this->query_list[&#39;having&#39;])?($select_sql.=&#39; mysql having &#39;.$this->query_list[&#39;having&#39;]):&#39;&#39;;
        isset($this->query_list[&#39;order&#39;])?($select_sql.=&#39; order by &#39;.$this->query_list[&#39;order&#39;]):&#39;&#39;;
        isset($this->query_list[&#39;limit&#39;])?($select_sql.=&#39; &#39;.$this->query_list[&#39;limit&#39;]):&#39;&#39;;
        
        return $this->query($select_sql);
    }
    /**
     +----------------------------------------------------------
     * 增加
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
    public function add(){
        $add_sql = &#39;insert into `&#39;.$this->query_list[&#39;table&#39;].&#39;` (&#39;;
        
        $data = $this->query_list[&#39;data&#39;];
        $value = $field = &#39;&#39;;
        foreach($data as $k=>$v){
            $field .= &#39;`&#39;.$k.&#39;`,&#39;;
            if(is_numeric($v))
                $value .= $v.&#39;,&#39;;
            else
                $value .= &#39;\&#39;&#39;.$v.&#39;\&#39;,&#39;;
        }
        $add_sql .= rtrim($field,&#39;,&#39;).&#39;) values (&#39;.rtrim($value,&#39;,&#39;).&#39;)&#39;;
        return $this->execute($add_sql);
    }
    /**
     +----------------------------------------------------------
     * 删除
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
    public function delete(){
        $del_sql = &#39;delete from `&#39;.$this->query_list[&#39;table&#39;].&#39;` where &#39;.$this->query_list[&#39;where&#39;];
        
        if(isset($this->query_list[&#39;order&#39;]))
            $del_sql .= &#39;order by &#39;.$this->query_list[&#39;order&#39;];
        if(isset($this->query_list[&#39;limit&#39;]))
            $del_sql .= &#39; &#39;.$this->query_list[&#39;limit&#39;];
            
        return $this->execute($del_sql);
        
    }
    /**
     +----------------------------------------------------------
     * 更新
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
    public function update(){
        $update_sql = &#39;update `&#39;.$this->query_list[&#39;table&#39;].&#39;` set &#39;;
        $data = $this->query_list[&#39;data&#39;];
        
        foreach($data as $k=>$v){
            if(is_numeric($v))
                $update_sql .= &#39;`&#39;.$k.&#39;` =&#39;.$v.&#39;,&#39;;
            else
                $update_sql .= &#39;`&#39;.$k.&#39;` =\&#39;&#39;.$v.&#39;\&#39;,&#39;;
        }
        $update_sql = rtrim($update_sql,&#39;,&#39;);
        if(isset($this->query_list[&#39;where&#39;]))
            $update_sql .= &#39; where &#39;.$this->query_list[&#39;where&#39;];
        if(isset($this->query_list[&#39;order&#39;]))
            $update_sql .= &#39; order by &#39;.$this->query_list[&#39;order&#39;];
        if(isset($this->query_list[&#39;limit&#39;]))
            $update_sql .= &#39; &#39;.$this->query_list[&#39;limit&#39;];
        
        return $this->execute($update_sql);
        
    }
     /**
     +----------------------------------------------------------
     * 执行查询 返回数据集
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $sql  sql指令
     */
    public function query($sql) {
        if ( !$this->conn ) return false;
        $this->queryStr = $sql;
        //释放前次的查询结果
        if ( $this->queryID ) {    $this->free();    }
        
        $this->query_start_time = microtime(true);
        
        $this->queryID = mysql_query($sql, $this->conn);
        $this->query_count++;
        if ( false === $this->queryID ) {
            $this->error();
            return false;
        } else {
            $this->numRows = mysql_num_rows($this->queryID);
            return $this->getAll();
        }
    }
    /**
     +----------------------------------------------------------
     * 执行语句
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $sql  sql指令
     +----------------------------------------------------------
     */
    public function execute($sql) {
        if ( !$this->conn ) return false;
        $this->queryStr = $sql;
        //释放前次的查询结果
        if ( $this->queryID ) {    $this->free();    }
        
        $this->query_start_time = microtime(true);
        
        $result =   mysql_query($sql, $this->conn) ;
        $this->query_count++;
        if ( false === $result) {
            $this->error();
            return false;
        } else {
            $this->numRows = mysql_affected_rows($this->conn);
            return $this->numRows;
        }
    }
    /**
     +----------------------------------------------------------
     * 获得所有的查询数据
     +----------------------------------------------------------
     * @access private
     +----------------------------------------------------------
     * @return array
     */
    private function getAll() {
        //返回数据集
        $result = array();
        if($this->numRows >0) {
            while($row = mysql_fetch_assoc($this->queryID)){
                $result[]   =   $row;
            }
            mysql_data_seek($this->queryID,0);
        }
        return $result;
    }
    /**
     +----------------------------------------------------------
     * 取得数据表的字段信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function getFields($tableName) {
        $result =   $this->query(&#39;SHOW COLUMNS FROM `&#39;.$tableName.&#39;`&#39;);
        $info   =   array();
        if($result) {
            foreach ($result as $key => $val) {
                $info[$val[&#39;Field&#39;]] = array(
                    &#39;name&#39;    => $val[&#39;Field&#39;],
                    &#39;type&#39;    => $val[&#39;Type&#39;],
                    &#39;notnull&#39; => (bool) ($val[&#39;Null&#39;] === &#39;&#39;), // not null is empty, null is yes
                    &#39;default&#39; => $val[&#39;Default&#39;],
                    &#39;primary&#39; => (strtolower($val[&#39;Key&#39;]) == &#39;pri&#39;),
                    &#39;autoinc&#39; => (strtolower($val[&#39;Extra&#39;]) == &#39;auto_increment&#39;),
                );
            }
        }
        return $info;
    }
    /**
     +----------------------------------------------------------
     * 取得数据库的表信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function getTables($dbName=&#39;&#39;) {
        if(!empty($dbName)) {
           $sql    = &#39;SHOW TABLES FROM &#39;.$dbName;
        }else{
           $sql    = &#39;SHOW TABLES &#39;;
        }
        $result =   $this->query($sql);
        $info   =   array();
        foreach ($result as $key => $val) {
            $info[$key] = current($val);
        }
        return $info;
    }
    /**
     +----------------------------------------------------------
     * 最后次操作的ID
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
     public function last_insert_id(){
        return mysql_insert_id($this->conn);
    }
    /**
     * 执行一条带有结果集计数的
     */
    public function count($sql){
        return $this->execute($sql);
    }
    /**
     +----------------------------------------------------------
     * 启动事务
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public function startTrans() {
        if ($this->transTimes == 0) {
            mysql_query(&#39;START TRANSACTION&#39;, $this->conn);
        }
        $this->transTimes++;
        return ;
    }
    /**
     +----------------------------------------------------------
     * 提交事务
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    public function commit()
    {
        if ($this->transTimes > 0) {
            $result = mysql_query(&#39;COMMIT&#39;, $this->conn);
            $this->transTimes = 0;
            if(!$result){
                throw new Exception($this->error());
            }
        }
        return true;
    }
    /**
     +----------------------------------------------------------
     * 事务回滚
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return boolen
     +----------------------------------------------------------
     */
    public function rollback()
    {
        if ($this->transTimes > 0) {
            $result = mysql_query(&#39;ROLLBACK&#39;, $this->conn);
            $this->transTimes = 0;
            if(!$result){
                throw new Exception($this->error());
            }
        }
        return true;
    }
    /**
     +----------------------------------------------------------
     * 错误信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
     public function error() {
        $this->error = mysql_error($this->conn);
        if(&#39;&#39; != $this->queryStr){
            $this->error .= "\n [ SQL语句 ] : ".$this->queryStr;
        }
        return $this->error;
    }
    /**
     +----------------------------------------------------------
     * 释放查询结果
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    public function free() {
        @mysql_free_result($this->queryID);
        $this->queryID = 0;
        $this->query_list = null;
    }
    /**
     +----------------------------------------------------------
     * 关闭连接
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param 
     +----------------------------------------------------------
     */
    function close(){
        if ($this->conn && !mysql_close($this->conn)){
            throw new Exception($this->error());
        }
        $this->cOnn= 0;
        $this->query_count = 0;
    }
    /**
     +----------------------------------------------------------
     * 析构方法
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     */
    function __destruct(){
         $this->close();
    }
}

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • yum安装_Redis —yum安装全过程
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Redis—yum安装全过程相关的知识,希望对你有一定的参考价值。访问https://redi ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • 本文介绍了通过mysql命令查看mysql的安装路径的方法,提供了相应的sql语句,并希望对读者有参考价值。 ... [详细]
author-avatar
我叫yyson_836
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有