一个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 相关文章推荐
PHP header函数分析详解
Aug 06 PHP
Eclipse中php插件安装及Xdebug配置的使用详解
Apr 25 PHP
php生成随机数的三种方法
Sep 10 PHP
php json转换成数组形式代码分享
Nov 10 PHP
php找出指定范围内回文数且平方根也是回文数的方法
Mar 23 PHP
实现WordPress主题侧边栏切换功能的PHP脚本详解
Dec 14 PHP
php实现微信公众号主动推送消息
Dec 31 PHP
如何解决PHP使用mysql_query查询超大结果集超内存问题
Mar 14 PHP
PHP安全下载文件的方法
Apr 07 PHP
php上传大文件设置方法
Apr 14 PHP
老生常谈文本文件和二进制文件的区别
Feb 27 PHP
基于laravel belongsTo使用详解
Oct 18 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
windows下zendframework项目环境搭建(通过命令行配置)
2012/12/06 PHP
浅析ThinkPHP中execute和query方法的区别
2014/06/13 PHP
完善CodeIgniter在IDE中代码提示功能的方法
2014/07/19 PHP
异步加载技术实现当滚动条到最底部的瀑布流效果
2014/09/16 PHP
php实现表单多按钮提交action的处理方法
2015/10/24 PHP
php版微信公众号接口实现发红包的方法
2016/10/14 PHP
phpStudy配置多站点多域名和多端口的方法
2017/09/01 PHP
微信公众平台开发教程①获取用户Openid及个人信息图文详解
2019/04/10 PHP
PHP字符串中抽取子串操作实例分析
2019/06/22 PHP
PHP使用gearman进行异步的邮件或短信发送操作详解
2020/02/27 PHP
tp5.1 框架路由操作-URL生成实例分析
2020/05/26 PHP
很酷的javascript loading效果代码
2008/06/18 Javascript
JavaScript 通过模式匹配实现重载
2010/08/12 Javascript
基于jquery自定义图片热区效果
2012/07/21 Javascript
调整小数的格式保留小数点后两位
2014/05/14 Javascript
JQuery遍历json数组的3种方法
2014/11/08 Javascript
js动态切换图片的方法
2015/01/20 Javascript
jquery实现邮箱自动填充提示功能
2015/11/17 Javascript
JS中的二叉树遍历详解
2016/03/18 Javascript
jquery制做精致的倒计时特效
2016/06/13 Javascript
jQuery实现的事件绑定功能基本示例
2017/10/11 jQuery
vue使用echarts图表自适应的几种解决方案
2020/12/04 Vue.js
[01:10]为家乡而战!完美世界城市挑战赛全国总决赛花絮
2019/07/25 DOTA
如何运行Python程序的方法
2013/04/21 Python
基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解
2017/10/13 Python
Python实现压缩文件夹与解压缩zip文件的方法
2018/09/01 Python
浅析python的优势和不足之处
2018/11/20 Python
使用Dajngo 通过代码添加xadmin用户和权限(组)
2020/07/03 Python
英国在线定制百叶窗网站:Swift Direct Blinds
2020/02/25 全球购物
团支书的期末学习总结自我评价
2013/11/01 职场文书
超市督导岗位职责
2015/04/10 职场文书
本科毕业论文答辩稿
2015/06/23 职场文书
2016年学生会感恩节活动总结
2016/04/01 职场文书
解决pycharm下载库时出现Failed to install package的问题
2021/09/04 Python
ICOM R71E和R72E图文对比解说
2022/04/07 无线电