一个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原理之异常机制深入分析
Aug 08 PHP
linux下为php添加curl扩展的方法
Jul 29 PHP
php使HTML标签自动补全闭合函数代码
Oct 04 PHP
php使用array_rand()函数从数组中随机选择一个或多个元素
Apr 28 PHP
win7计划任务定时执行PHP脚本设置图解
May 09 PHP
php获取指定日期之间的各个周和月的起止时间
Nov 24 PHP
php匹配字符中链接地址的方法
Dec 22 PHP
PHP使用自定义方法实现数组合并示例
Jul 07 PHP
在Thinkphp中使用ajax实现无刷新分页的方法
Oct 25 PHP
PHP之将POST数据转化为字符串的实现代码
Nov 03 PHP
php 生成加密公钥加密私钥实例详解
Jun 16 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
Aug 31 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
PHP设计模式之代理模式的深入解析
2013/06/13 PHP
PHP实现适用于自定义的验证码类
2016/06/15 PHP
Yii2.0建立公共方法简单示例
2019/01/29 PHP
PhpStorm配置Xdebug调试的方法步骤
2019/02/02 PHP
newxtree.js代码
2007/03/13 Javascript
JavaScript 组件之旅(二)编码实现和算法
2009/10/28 Javascript
js 在定义的时候立即执行的函数表达式(function)写法
2013/01/16 Javascript
js中call与apply的用法小结
2013/12/28 Javascript
jquery实现submit提交表单
2015/02/03 Javascript
JavaScript中匿名函数用法实例
2015/03/23 Javascript
将JavaScript的jQuery库中表单转化为JSON对象的方法
2015/11/17 Javascript
JavaScript中捕获与冒泡详解及实例
2017/02/03 Javascript
JavaScript中的编码和解码函数
2017/02/15 Javascript
js简易版购物车功能
2017/06/17 Javascript
浅析Javascript中双等号(==)隐性转换机制
2017/10/27 Javascript
JS 使用 window对象的print方法实现分页打印功能
2018/05/16 Javascript
解决vue中修改了数据但视图无法更新的情况
2018/08/27 Javascript
[08:04]TI4西雅图DOTA2前线报道 海涛探访各路人马
2014/07/09 DOTA
[45:34]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.18
2020/12/19 DOTA
python网络编程之TCP通信实例和socketserver框架使用例子
2014/04/25 Python
Python中用startswith()函数判断字符串开头的教程
2015/04/07 Python
总结网络IO模型与select模型的Python实例讲解
2016/06/27 Python
Python网络爬虫项目:内容提取器的定义
2016/10/25 Python
PyCharm配置mongo插件的方法
2018/11/30 Python
python twilio模块实现发送手机短信功能
2019/08/02 Python
Python的pygame安装教程详解
2020/02/10 Python
python 操作mysql数据中fetchone()和fetchall()方式
2020/05/15 Python
详解Python的爬虫框架 Scrapy
2020/08/03 Python
使用分层画布来优化HTML5渲染的教程
2015/05/08 HTML / CSS
微型企业创业投资计划书
2014/01/10 职场文书
中式餐厅创业计划书范文
2014/01/23 职场文书
大学活动总结范文
2014/04/29 职场文书
2014年生活老师工作总结
2014/12/23 职场文书
行政主管岗位职责范本
2015/04/09 职场文书
车位出租协议书范本
2016/03/19 职场文书
奇妙的 CSS shapes(CSS图形)
2021/04/05 HTML / CSS