zoukankan      html  css  js  c++  java
  • php 如何造一个简短原始的数据库类用来增加工作效率

    class DBDA
    {
     public $host="localhost";
     public $uid="root";
     public $pwd="123";
     public $dbname="mydb";
     
     /**
     *给一个sql语句,返回执行的结果
     *@param string $sql 用户指定的sql语句
     *@param int $type 用户给的语句类型,0代表增删改,1代表查询
     *@return  返回查询的结果,如果是查询返回二维数组,如果是增删改返回true或false
     */
     function Query($sql,$type=1)           //type默认是1,在sql语句是增删改的情况下一定不要忘记将type的参数写成0
     {
      //造连接对象
      $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
      
      //执行sql语句
      $reslut = $db->query($sql);
      
      //从结果集对象里面取数据
      if($type==1)
      {
       return $reslut->fetch_all();
      }
      else
      {
       return $reslut;
      }
     }
     
     /**
     *给一个sql语句,返回关联的二维数组
     *@param string $sql 用户指定的sql语句
     *@param int $type 用户给的语句类型,0代表增删改,1代表查询
     *@return  返回查询的结果,如果是查询返回二维数组,如果是增删改返回true或false
     */
     function GuanQuery($sql,$type=1)
     {
      //造连接对象
      $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
      
      //执行sql语句
      $reslut = $db->query($sql);
      
      //取数据
      if($type==1)
      {
       $attr = array();
       while($a = $reslut->fetch_assoc())
       {
        $attr[] = $a;
       }
       
       return $attr; 
      }
      else
      {
       return $reslut;
      }
     }
     /**
     *给一个sql语句,返回字符串
     *@param string $sql 用户指定的sql语句
     *@param int $type 用户给的语句类型,0代表增删改,1代表查询
     *@return  返回查询的结果,如果是查询返回字符串,如果是增删改返回true或false
     */
     function StrQuery($sql,$type=1)      //单个数据的情况下strquery可直接给出结果,同时适合与聚合函数
     {
      //造连接对象
      $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
      
      //执行sql语句
      $reslut = $db->query($sql);
      
      //取数据
      if($type==1)
      {
       $attr = $reslut->fetch_all();
       $str="";
       foreach($attr as $v)
       {
        $str .= implode("^",$v);                              //字符串拼接
        $str .="|";
       }
       return substr($str,0,strlen($str)-1);               //去掉最后的"|"
      }
      else
      {
       return $reslut;
      }
     }
    }

  • 相关阅读:
    Mysql常见索引介绍
    Mysql字段修饰符(约束)
    使用select和show命令查看mysql数据库系统信息
    Mysql5.7数据库介绍
    对Mysql数据表本身进行操作
    各种修改Mysql字符集
    Mysql的安全配置向导命令mysql_secure_installation
    firewalld介绍
    CentOS7使用yum安装mysql5.7
    利用ASP.NET一般处理程序动态生成Web图像(转)
  • 原文地址:https://www.cnblogs.com/bujianchenxi/p/6041558.html
Copyright © 2011-2022 走看看