一个php Mysql类 可以参考学习熟悉下


Posted in PHP onJune 21, 2009
<?php 
class Mysql 
{ 
private $conn; 
private $host; 
private $username; 
private $password; 
private $dbname; 
private $pconnect; 
private $charset; public function __construct(array $params = null) 
{ 
if (!empty($params)) { 
foreach ($params as $k => $v) { 
$this->$k = $v; 
} 
} 
} 
public function connect() 
{ 
$fun = $this->pconnect ? 'mysql_pconnect' : 'mysql_connect'; 
$this->conn = $fun($this->host, $this->username, $this->password); 
$this->conn && $this->query('set names ' . $this->charset); 
$this->conn && mysql_select_db($this->dbname, $this->conn); 
} 
public function getInstance() 
{ 
return $this->conn; 
} 
public function query($sql) 
{ 
return mysql_query($sql, $this->conn); 
} 
public function fetchOne($sql) 
{ 
$data = $this->fetchRow($sql); 
return $data[0]; 
} 
public function fetchCol($sql) 
{ 
$tmp = $this->fetchAll($sql, MYSQL_NUM); 
foreach ($tmp as $v) { 
$data[] = $v[0]; 
} 
} 
public function fetchRow($sql) 
{ 
$result = $this->query($sql); 
$data = mysql_fetch_row($result); 
mysql_free_result($result); 
return $data; 
} 
public function fetchAssoc($sql) 
{ 
$result = $this->query($sql); 
$data = mysql_fetch_assoc($result); 
mysql_free_result($result); 
return $data; 
} 
public function fetchAll($sql, $type = MYSQL_ASSOC) 
{ 
$result = $this->query($sql); 
while ($tmp = mysql_fetch_array($result, $type)) { 
$data[] = $tmp; 
} 
return $data; 
} 
public function fetchPairs($sql) 
{ 
$result = $this->query($sql); 
while ($tmp = mysql_fetch_row($result)) { 
$data[$tmp[0]] = $tmp[1]; 
} 
return $data; 
} 
public function insert($table, array $bind) 
{ 
$cols = array(); 
$vals = array(); 
foreach ($bind as $col => $val) { 
$cols[] = $col; 
$vals[] = $val; 
unset($bind[$col]); 
} 
$sql = "INSERT INTO " 
. $table 
. ' (`' . implode('`, `', $cols) . '`) ' 
. 'VALUES (\'' . implode('\', \'', $vals) . '\')'; 
$stmt = $this->query($sql, $this->conn); 
$result = $this->affectedRows(); 
return $result; 
} 
public function getLastInsertId() 
{ 
return mysql_insert_id($this->conn); 
} 
public function affectedRows() 
{ 
return mysql_affected_rows($this->conn); 
} 
public function update($table, array $bind, $where = '') 
{ 
$set = array(); 
foreach ($bind as $col => $val) { 
$set[] = '`' . $col . "` = '" . $val . "'"; 
} 
$sql = "UPDATE `" 
. $table 
. '` SET ' . implode(', ', $set) 
. (($where) ? " WHERE $where" : ''); 
$stmt = $this->query($sql, array_values($bind)); 
$result = $this->affectedRows(); 
return $result; 
} 
public function delete($table, $where = '') 
{ 
/** 
* Build the DELETE statement 
*/ 
$sql = "DELETE FROM " 
. $table 
. (($where) ? " WHERE $where" : ''); 
/** 
* Execute the statement and return the number of affected rows 
*/ 
$stmt = $this->query($sql); 
$result = $stmt ? mysql_affected_rows($this->conn) : $stmt; 
return $result; 
} 
public function close() 
{ 
$this->conn && mysql_close($this->conn); 
} 
} 
?>
PHP 相关文章推荐
桌面中心(二)数据库写入
Oct 09 PHP
教你如何把一篇文章按要求分段
Oct 09 PHP
php5 图片验证码实现代码
Dec 11 PHP
关于PHP的相似度计算函数:levenshtein的使用介绍
Apr 15 PHP
那些年我们错过的魔术方法(Magic Methods)
Jan 14 PHP
ThinkPHP缓存方法S()概述
Jun 13 PHP
PHP清除字符串中所有无用标签的方法
Dec 01 PHP
php插入排序法实现数组排序实例
Feb 16 PHP
PHP+MYSQL中文乱码问题
Jul 01 PHP
Zend Framework基本页面布局分析
Mar 19 PHP
php从身份证获取性别和出生年月
Feb 09 PHP
PHP实现用户登录的案例代码
May 10 PHP
discuz7 phpMysql操作类
Jun 21 #PHP
php 将bmp图片转为jpg等其他任意格式的图片
Jun 21 #PHP
ie6 动态缩略图不显示的原因
Jun 21 #PHP
PHP COOKIE设置为浏览器进程
Jun 21 #PHP
PHP 输出缓存详解
Jun 20 #PHP
php 图像函数大举例(非原创)
Jun 20 #PHP
PHP 类型转换函数intval
Jun 20 #PHP
You might like
雄兵连:天使彦天使彦为爱折翼,彦和炙心同时念动的誓言!
2020/03/02 国漫
ThinkPHP快速入门实例教程之数据分页
2014/07/01 PHP
谈谈 PHP7新增功能
2015/12/16 PHP
Ajax实现对静态页面的文章访问统计功能示例
2016/10/10 PHP
PHP批量删除jQuery操作
2017/07/23 PHP
掌握PHP垃圾回收机制详解
2019/03/13 PHP
[原创]提供复制本站内容时出现,该文章转自脚本之家等字样的js代码
2007/03/27 Javascript
myEvent.js javascript跨浏览器事件框架
2011/10/24 Javascript
jQuery插件StickUp实现网页导航置顶
2015/04/12 Javascript
JS实现随机乱撞彩色圆球特效的方法
2015/05/05 Javascript
jquery实现左右无缝轮播图
2020/07/31 Javascript
AngularJS基础 ng-options 指令详解
2016/08/02 Javascript
极力推荐10个短小实用的JavaScript代码段
2016/08/03 Javascript
AngularJS 表达式详解及实例代码
2016/09/14 Javascript
React native ListView 增加顶部下拉刷新和底下点击刷新示例
2018/04/27 Javascript
Vue表单输入绑定的示例代码
2018/11/01 Javascript
JavaScript解析JSON数据示例
2019/07/16 Javascript
记录微信小程序 height: calc(xx - xx);无效问题
2019/12/30 Javascript
使用JavaScript获取Django模板指定键值数据
2020/05/27 Javascript
vscode+gulp轻松开发小程序的完整步骤
2020/10/18 Javascript
[54:02]2018DOTA2亚洲邀请赛 4.1 小组赛 B组 IG vs VGJ.T
2018/04/03 DOTA
用Python生成器实现微线程编程的教程
2015/04/13 Python
python开发之文件操作用法实例
2015/11/13 Python
python下setuptools的安装详解及No module named setuptools的解决方法
2017/07/06 Python
浅析python参数的知识点
2018/12/10 Python
python命令行参数用法实例分析
2019/06/25 Python
Python如何实现动态数组
2019/11/02 Python
基于Python中isfile函数和isdir函数使用详解
2019/11/29 Python
Django中的AutoField字段使用
2020/05/18 Python
一款纯css3实现的非常实用的鼠标悬停特效演示
2014/11/05 HTML / CSS
澳大利亚商务邀请函
2014/01/17 职场文书
优秀党支部书记事迹材料
2014/05/29 职场文书
大三学年自我鉴定范文(3篇)
2014/09/28 职场文书
学习保证书100字
2015/02/26 职场文书
SpringBoot工程下使用OpenFeign的坑及解决
2021/07/02 Java/Android
解决Mysql报错 Table 'mysql.user' doesn't exist
2022/05/06 MySQL