Home > Database > Mysql Tutorial > PHP工具类 MySQL增删改查工具类_MySQL

PHP工具类 MySQL增删改查工具类_MySQL

WBOY
Release: 2016-05-27 13:46:34
Original
1312 people have browsed it

?php
header("content-type:text/html;charset=utf-8");
class DBUtils{
     
     
 /**
  *通用更新方法 insert update delete 操作
  *@param sql  
  *@return bool  true false
  */
  public function update($sql){
    $link = $this->getConn();
    mysql_query($sql);
    //如果出错显示
   if(DEBUG){
   echo mysql_error();
   }
    $rs = mysql_affected_rows($link);
    $rs = $rs > 0;
    mysql_close($link);
    return $rs;
  }
     
 /**
  *通用查询方法 select 操作
  *@param sql  
  *@return array
  */
  public function queryRows($sql){
   //创建连接,编码,数据库
   $link = $this->getConn();
   //发送sql
   $rs = mysql_query($sql);
   //如果出错显示
   if(DEBUG){
   echo mysql_error();
   }
       
       
   $rows = array();
   while($row = mysql_fetch_array($rs)){
    $rows[] = $row;//pdemo7.php
   }
   //
   mysql_free_result($rs);    
   mysql_close($link);
   return $rows;
  }
     
        
 /**
  *通用查询方法 select 操作  查询结果一行数据
  *@param sql  
  *@return array   如果失败返回 false;
  */
 public function queryRow($sql){
    $rs = $this->queryRows($sql);
    if(!empty($rs[0])){
     return $rs[0];
    }
    return false;
 }
     
 /**
  *通用查询方法 select 操作  查询结果一个数据
  *@param sql  
  *@return array   如果失败返回 false;
  * 例:  select count(*) from user;
  */
 public function queryObj($sql){
     $rs = $this->queryRows($sql);
    //var_dump($rs);
    if(!empty($rs[0][0])){
     return $rs[0][0];
    }
    return false;
 }
     
    
  private function getConn(){
   $link = mysql_connect('127.0.0.1','root','');
   mysql_query("set names utf8");
   mysql_select_db("news");
   return $link;
  }
     
}
Copy after login

 

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template