一个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数据库连接
Oct 09 PHP
利用 window_onload 实现select默认选择
Oct 09 PHP
使用adodb lite解决问题
Dec 31 PHP
生成卡号php代码
Apr 09 PHP
PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍
Sep 11 PHP
yii框架表单模型使用及以数组形式提交表单数据示例
Apr 30 PHP
PHP中调用SVN命令更新网站方法
Jan 07 PHP
PHP中CheckBox多选框上传失败的代码写法
Feb 13 PHP
PHP迭代与递归实现无限级分类
Aug 28 PHP
php微信开发之图片回复功能
Jun 14 PHP
laravel excel 上传文件保存到本地服务器功能
Nov 14 PHP
PHP http请求超时问题解决方案
Nov 13 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
IStream与TStream之间的相互转换
2008/08/01 PHP
php使用websocket示例详解
2014/03/12 PHP
windows下配置apache+php+mysql时出现问题的处理方法
2014/06/20 PHP
作为程序员必知的16个最佳PHP库
2015/12/09 PHP
Smarty实现页面静态化(生成HTML)的方法
2016/05/23 PHP
php设计模式之迭代器模式实例分析【星际争霸游戏案例】
2020/04/07 PHP
为数据添加append,remove功能
2006/10/03 Javascript
js函数调用常用方法详解
2012/12/03 Javascript
Js日期选择器并自动加入到输入框中示例代码
2013/08/02 Javascript
实现动画效果核心方式的js代码
2013/09/27 Javascript
在Google 地图上实现做的标记相连接
2015/01/05 Javascript
如何使用AngularJs打造权限管理系统【简易型】
2016/05/09 Javascript
jquery自定义插件结合baiduTemplate.js实现异步刷新(附源码)
2016/12/22 Javascript
详解Angular中的自定义服务Service、Provider以及Factory
2017/04/22 Javascript
从setTimeout看js函数执行过程
2017/12/19 Javascript
js实现贪吃蛇游戏(简易版)
2020/09/29 Javascript
vue使用Sass时报错问题的解决方法
2020/10/14 Javascript
[02:27]刀塔重生降临
2015/10/14 DOTA
[56:13]DOTA2-DPC中国联赛定级赛 LBZS vs Phoenix BO3第一场 1月10日
2021/03/11 DOTA
Python对象体系深入分析
2014/10/28 Python
使用Python脚本操作MongoDB的教程
2015/04/16 Python
python实现根据主机名字获得所有ip地址的方法
2015/06/28 Python
Python二维码生成识别实例详解
2019/07/16 Python
python实例化对象的具体方法
2020/06/17 Python
深入理解Python变量的数据类型和存储
2021/02/01 Python
详解基于 Canvas 手撸一个六边形能力图
2019/09/02 HTML / CSS
html5的自定义data-*属性与jquery的data()方法的使用
2014/07/02 HTML / CSS
项目副经理岗位职责
2013/12/30 职场文书
感恩节活动方案
2014/01/27 职场文书
专业求职信撰写要诀
2014/02/18 职场文书
2014全国两会学习心得体会2000字
2014/03/10 职场文书
益达广告词
2014/03/14 职场文书
研究生考核个人自我鉴定
2014/03/27 职场文书
卫生院艾滋病宣传活动小结
2014/07/09 职场文书
党的群众路线教育实践活动自我剖析材料
2014/10/08 职场文书
i7 6700处理器相当于i5几代
2022/04/19 数码科技