PHP使用mysqli扩展库实现增删改查(面向对象版)
2013-09-17 22:42
513 查看
mysqli扩展库是mysql扩展库的改进版本,在mysql扩展库的基础上提高了稳定性和效率,mysqli扩展库有两套东西,一套就是面向过程的mysqli另一套是面向对象的mysqli。操作方式大体和mysql扩展库大体一致,这次还是先抽取出来一个操作mysql的工具类,和调用的类。
1.mysqli扩展库操作数据库工具类
2.下面是具体的调用工具类的代码
如果要用到其他方法可以查阅php开发文档。
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(); ?>
如果要用到其他方法可以查阅php开发文档。
相关文章推荐
- PHP使用mysqli扩展库实现增删改查(面向对象版)
- PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty)
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
- 使用PHP连接数据库_实现用户数据的增删改查的整体操作示例
- js中实现cookie的增删改查(document.cookie的使用详情)
- 一个使用Pro*C实现增删改查的小例子
- 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类
- 使用springmvc+mybatis实现增删改查并进行登录注册功能
- 一个使用Pro*C实现增删改查的小例子
- PHP实现对xml的增删改查操作案例分析
- 使用Enitity Framework实现增删改查服务中的一些通用思路
- js中实现cookie的增删改查(document.cookie的使用详情)
- salesforce 零基础学习(五十一)使用 Salesforce.com SOAP API 实现用户登录以及简单的增删改查(JAVA访问salesforce)
- Android开发(10)使用SQLiteDatabase操作SQLite数据库实现增删改查(源码)
- PHP基于PDO实现的SQLite操作类【包含增删改查及事务等操作】
- 使用VS2015和Nhibernate实现与MySql数据库连接,实现增删改查操作
- Greendao 简单实现增删改查使用过GreenDao的同学都知道,3.0之前需要通过新建GreenDaoGenerator工程生成Java数据对象(实体)和DAO对象,非常的繁琐而且也加大了使用成
- PHP实现对xml进行简单的增删改查(CRUD)操作示例
- js中实现cookie的增删改查(document.cookie的使用详情)