一个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 大数据量及海量数据处理算法总结
May 07 PHP
PHP5 的对象赋值机制介绍
Aug 02 PHP
PHP实现异步调用方法研究与分享
Oct 27 PHP
PHP转换文件夹下所有文件编码的实现代码
Jun 06 PHP
php fsockopen解决办法 php实现多线程
Jan 20 PHP
推荐25款php中非常有用的类库
Sep 29 PHP
PHP利用header跳转失效的解决方法
Oct 24 PHP
CentOS 7.2 下编译安装PHP7.0.10+MySQL5.7.14+Nginx1.10.1的方法详解(mini版本)
Sep 01 PHP
PHP回调函数简单用法示例
May 08 PHP
实例分析10个PHP常见安全问题
Jul 09 PHP
laravel http 自定义公共验证和响应的方法
Sep 29 PHP
PHP与Web页面的交互示例详解二
Aug 04 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实现的在线人员函数库
2008/04/09 PHP
php中比较简单的导入phpmyadmin生成的sql文件的方法
2011/06/28 PHP
使用PHP遍历文件夹与子目录的函数代码
2011/09/26 PHP
PHP微框架Dispatch简介
2014/06/12 PHP
PHP.vs.JAVA
2016/04/29 PHP
js实现的网站首页随机公告随机公告
2007/03/14 Javascript
NodeJs中的非阻塞方法介绍
2012/06/05 NodeJs
jquery简单瀑布流实现原理及ie8下测试代码
2013/01/23 Javascript
jQuery 无限级菜单的简单实例
2014/02/21 Javascript
当前流行的JavaScript代码风格指南
2014/09/10 Javascript
jQuery中trigger()方法用法实例
2015/01/19 Javascript
jquery仅用6行代码实现滑动门效果
2015/09/07 Javascript
js浏览器html5表单验证
2016/10/17 Javascript
基于jQuery实现瀑布流页面
2017/04/11 jQuery
JavaScript对象_动力节点Java学院整理
2017/06/23 Javascript
详解angular路由高亮之RouterLinkActive
2018/04/28 Javascript
JS实现点击按钮可实现编辑功能
2018/07/03 Javascript
JavaScript获取当前url路径过程解析
2019/12/27 Javascript
[00:43]拉比克至宝魔导师密钥展示
2018/12/20 DOTA
PyCharm使用教程之搭建Python开发环境
2016/06/07 Python
Python连接phoenix的方法示例
2017/09/29 Python
python3 破解 geetest(极验)的滑块验证码功能
2018/02/24 Python
Python生态圈图像格式转换问题(推荐)
2019/12/02 Python
python实现全排列代码(回溯、深度优先搜索)
2020/02/26 Python
使用SQLAlchemy操作数据库表过程解析
2020/06/10 Python
优衣库英国官网:UNIQLO英国
2016/12/25 全球购物
警校毕业生自我评价
2014/04/06 职场文书
学校安全教育月活动总结
2014/07/07 职场文书
校园文化艺术节宣传标语
2014/10/09 职场文书
学习党史心得体会2016
2016/01/23 职场文书
财产分割协议书
2016/03/22 职场文书
优秀大学生申请书
2019/06/24 职场文书
写给消防战士们的一封慰问信
2019/10/07 职场文书
Python绘制分类图的方法
2021/04/20 Python
golang 如何通过反射创建新对象
2021/04/28 Golang
python常见的占位符总结及用法
2021/07/02 Python