分享一个本人一直在使用的一个mysql数据库操作类,比较初级,自己给别人做web网站的时候一直用的这个类,这个也是之前刚开始学习php的时候在网上找到的一个mysql数据库操作类,后来自己改了一些,比如加入了单例类的模式,自我感觉还是非常不错的一个操作mysql数据库的类吧,下面直接贴类代码和类的使用方法。

提示:该类已经升级,具体可参考本站文章:MySQL数据库连接类

php MySQL数据库操作类源代码:

class MySQL{

private $host; //服务器地址

private $name; //登录账号

private $pwd; //登录密码

private $dBase; //数据库名称

private $conn; //数据库链接资源

private $result; //结果集

private $msg; //返回结果

private $fields;//返回字段

private $fieldsNum; //返回字段数

private $rowsNum; //返回结果数

private $rowsRst; //返回单条记录的字段数组

private $filesArray = array();//返回字段数组

private $rowsArray = array();//返回结果数组

private $charset='utf8'; //设置操作的字符集

private $query_count=0; //查询结果次数

static private $_instance; //存储对象

//初始化类

private function __construct($host='',$name='',$pwd='',$dBase=''){

if($host != '') $this->host = $host;

if($name != '') $this->name = $name;

if($pwd != '') $this->pwd = $pwd;

if($dBase != '') $this->dBase = $dBase;

$this->init_conn();

}

//防止被克隆

private function __clone(){}

public static function getInstance($host='',$name='',$pwd='',$dBase=''){

if(FALSE == (self::$_instance instanceof self)){

self::$_instance = new self($host,$name,$pwd,$dBase);

}

return self::$_instance;

}

public function __set($name,$value){

$this->$name=$value;

}

public function __get($name){

return $this->$name;

}

//链接数据库

function init_conn(){

$this->conn=@mysql_connect($this->host,$this->name,$this->pwd) or die('connect db fail !');

@mysql_select_db($this->dBase,$this->conn) or die('select db fail !');

mysql_query("set names ".$this->charset);

}

//查询结果

function mysql_query_rst($sql){

if($this->conn == '') $this->init_conn();

$this->result = @mysql_query($sql,$this->conn);

$this->query_count++;

}

//取得字段数

function getFieldsNum($sql){

$this->mysql_query_rst($sql);

$this->fieldsNum = @mysql_num_fields($this->result);

}

//取得查询结果数

function getRowsNum($sql){

$this->mysql_query_rst($sql);

if(mysql_errno() == 0){

return @mysql_num_rows($this->result);

}else{

return '';

}

}

//取得记录数组(单条记录)

function getRowsRst($sql,$type=MYSQL_BOTH){

$this->mysql_query_rst($sql);

if(empty($this->result)) return '';

if(mysql_error() == 0){

$this->rowsRst = mysql_fetch_array($this->result,$type);

return $this->rowsRst;

}else{

return '';

}

}

//取得记录数组(多条记录)

function getRowsArray($sql,$type=MYSQL_BOTH){

!empty($this->rowsArray) ? $this->rowsArray=array() : '';

$this->mysql_query_rst($sql);

if(mysql_errno() == 0){

while($row = mysql_fetch_array($this->result,$type)) {

$this->rowsArray[] = $row;

}

return $this->rowsArray;

}else{

return '';

}

}

//更新、删除、添加记录数

function uidRst($sql){

if($this->conn == ''){

$this->init_conn();

}

@mysql_query($sql);

$this->rowsNum = @mysql_affected_rows();

if(mysql_errno() == 0){

return $this->rowsNum;

}else{

return '';

}

}

//返回最近插入的一条数据库的id值

function returnRstId($sql){

if($this->conn == ''){

$this->init_conn();

}

@mysql_query($sql);

if(mysql_errno() == 0){

return mysql_insert_id();

}else{

return '';

}

}

//获取对应的字段值

function getFields($sql,$fields){

$this->mysql_query_rst($sql);

if(mysql_errno() == 0){

if(mysql_num_rows($this->result) > 0){

$tmpfld = @mysql_fetch_row($this->result);

$this->fields = $tmpfld[$fields];

}

return $this->fields;

}else{

return '';

}

}

//错误信息

function msg_error(){

if(mysql_errno() != 0) {

$this->msg = mysql_error();

}

return $this->msg;

}

//释放结果集

function close_rst(){

mysql_free_result($this->result);

$this->msg = '';

$this->fieldsNum = 0;

$this->rowsNum = 0;

$this->filesArray = '';

$this->rowsArray = '';

}

//关闭数据库

function close_conn(){

$this->close_rst();

mysql_close($this->conn);

$this->conn = '';

}

//取得数据库版本

function db_version() {

return mysql_get_server_info();

}

}

调用方法如下:

include_once('mysql.class.php');

$db = MySQL::getInstance($db_host,$db_user,$db_pass,$db_data);

php mysql 数据库操作类_php mysql数据库操作类相关推荐

  1. mysql数据库永久链接_PHP使用数据库永久连接方式(mysql_pconnect)操作MySQL的是与非...

    作者:老王 PHP程序员应该都知道连接MySQL数据库可以使用mysql_pconnect(永久连接)函数,使用数据库永久连接可以提高效率,但是实际应用中数据库永久连接往往会导致出现一些问题,通常的表 ...

  2. php mysql实现下拉列表查询_php mysql如何实现通过下拉框查询显示数据库中的数据...

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  3. 怎样用mysql做留言板_PHP+Mysql 实现留言板

    最近看了下PHP基础语法,就想利用这些基本东西实现留言板,也是对基础知识的一个巩固. 什么是留言板?一种可以用来记录,展示文字信息的载体. 现切入正题,说说本次留言板是怎么实现! 首先用户提交留言后, ...

  4. php mysql blob存储图片_php mysql blob存储图片

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  5. php连接mysql执行sql语句_php+mysql 连接服务器、数据库以及执行SQL语句的类库

    展开全部 我这里有个很好的通用数据62616964757a686964616fe78988e69d8331333262363066库连接类,我就喜欢用这个,试试看. class DB_Handle{ ...

  6. php mysql 随机排序函数_php+mysql实现数据库随机重排实例

    本文实例实现了php+mysql数据库随机重排的方法,可将表中的所有数据随机读出来一次之后再进行随机保存到另一个表,从而达到了记录随机的功能. 主要实现代码如下: //数据库连接就不写在这里面了 $s ...

  7. mysql数据库无限分类_php+mysql数据库实现无限分类的方法

    本文实例讲述了php+mysql数据库实现无限分类的方法.分享给大家供大家参考.具体分析如下: 这款php无限分类代码比较完整理包括了数据库是mysql的,有增加.删除.编辑.移动的功能,同时还提供数 ...

  8. mysql创建数据库与表_PHP MySQL 创建数据库和表 之 Create

    创建数据库 CREATE DATABASE 语句用于在 MySQL 中创建数据库. 语法 CREATE DATABASE database_name 为了让 PHP 执行上面的语句,我们必须使用 my ...

  9. php连接mysql指定表名_php mysql获取指定数据库所有表名_PHP教程

    $cn = mysql_connect('localhost','root','root'); mysql_select_db('test',$cn); print_r(get_tables()); ...

最新文章

  1. php empty()和isset()的区别
  2. ubuntu服务器常见使用技巧及-kill掉后GPU显存不释放进程-
  3. 【五校联考5day1】序列
  4. OpenCV Tracker简介
  5. 公司重金求数据分析师:为什么90%的公司都需要它?
  6. 981. 基于时间的键值存储
  7. 合肥工贸高级技工学校计算机系,合肥工贸高级技工学校
  8. vue-router下的html5 history在iis服务器上的设置 vue去掉#
  9. android nsdservice 类型,Android NSD onServiceFound()没有被调用
  10. python手机版-QPython,一个在手机上运行Python的神器
  11. RabbitMQ之Topic交换器模式开发
  12. dpkg-checkbuilddeps: error: Unmet build dependencies: build-essential:native
  13. Indigo Untyped Channel
  14. 中国人口最多的姓氏排行
  15. linux dstat,dstat 用法详解
  16. sudo chmod 755 ....指令分析
  17. 【转载】深入浅出讲解FOC算法与SVPWM技术——自制FOC驱动器
  18. win7安装VScode(Visual Studio Code)
  19. Android实现60秒倒计时
  20. 【交易架构day10】阿里双十一交易核心数据及其它公司调研

热门文章

  1. 《城市建筑美学》读书笔记
  2. SMS短信的C语言代码摘抄
  3. mysql数据库提示本地无法连接远程服务器(Host is not allowed to connect to this MySQL server)解决办法
  4. c#中mysql远程连接方法及实例
  5. 怎么让datagridview中的某一行不可编辑
  6. 联想台式机网卡驱动_【装机帮扶站】第339期:联想刃7000:是否还有选购价值?4000价位装机推荐...
  7. 高校计算机课程建设研讨会通知,常熟理工学院新闻网
  8. python即时标记_【Python】读书笔记:Python基础教程-项目1-即时标记
  9. python axis 0_Python之NumPy(axis=0/1/2...)的透彻理解
  10. std::setprecision、std::ios::fixed使用说明