这里有新鲜出炉的Mysql教程,程序狗速度看过来!
MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。
这篇文章主要介绍了PHP实现基于面向对象的mysqli扩展库增删改查操作工具类,结合实例形式分析了mysqli增删改查操作类的封装与使用技巧,需要的朋友可以参考下
本文实例讲述了PHP实现基于面向对象的mysqli扩展库增删改查操作工具类。分享给大家供大家参考,具体如下:
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();
- ?>
如果要用到其他方法可以查阅php开发文档。
希望本文所述对大家PHP程序设计有所帮助。
来源: http://www.phperz.com/article/17/1024/350708.html