码迷,mamicode.com
首页 > 数据库 > 详细

PHP使用mysqli扩展库实现增删改查(面向对象版)

时间:2016-09-29 17:37:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

mysqli扩展库mysql扩展库的改进版本,在mysql扩展库的基础上提高了稳定性和效率,mysqli扩展库有两套东西,一套就是面向过程的mysqli另一套是面向对象的mysqli。操作方式大体和mysql扩展库大体一致,这次还是先抽取出来一个操作mysql的工具类,和调用的类。

1.mysqli扩展库操作数据库工具类

<?php
 //数据库操作类
  class DBUtil{
   private $host="localhost";
   private $username="root";
   private $password="123456";
   private $dbname="student";
   private $conn;
   public function DBUtil(){
     $this->conn=new mysqli($this->host, $this->username, $this->password,$this->dbname) or die($this->conn->connect_error);
    
   }
  //查询
   public function query($sql){
     $all= $this->conn->query($sql);
     return $all;
   }
  //插入,修改,删除
   public function otherOperate($sql){
      if($this->conn->query($sql)){
        if($this->conn->affected_rows>0){
           return "OK";
        }else{
           return "ERROOR";
        }
      }
   }
   public function close(){
     $this->conn->close();
   }
  }
?>

2.下面是具体的调用工具类的代码

<?php
  require_once "MySQLUtil.php";
   /*$sql="select * from m_student";
   $util=new DBUtil();
   $result=$util->query($sql);
   while($row=$result->fetch_assoc()){
        echo "$row[stuName]"."</br>";
   }
   $result->free();
   $util->close();*/
   $sql="update m_student set stuName=‘杨幂‘ where id=3";
   $util=new DBUtil();
   $result=$util->otherOperate($sql);
   echo $result;
   $util->close();
?>

参考阅读:www.manongjc.com/article/1206.html

PHP使用mysqli扩展库实现增删改查(面向对象版)

标签:

原文地址:http://www.cnblogs.com/myhomepages/p/5920496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!