一个php Mysql类 可以参考学习熟悉下
时间:2024-06-05 09:22:57
<?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,Mysql类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Nginx+Uwsgi+Django 项目部署到服务器的思路详解
2021-05-20 20:23:10
深入理解MySQL事务的4种隔离级别
2024-01-21 09:52:40
![](https://img.aspxhome.com/file/2023/2/118222_0s.jpg)
python 动态渲染 mysql 配置文件的示例
2024-01-26 05:15:20
![](https://img.aspxhome.com/file/2023/2/87972_0s.png)
详解Python给照片换底色(蓝底换红底)
2023-02-03 02:07:45
![](https://img.aspxhome.com/file/2023/5/134775_0s.png)
PHP 对象继承原理与简单用法示例
2023-11-21 21:57:32
mysql索引必须了解的几个重要问题
2024-01-27 17:58:54
在Python中操作文件之seek()方法的使用教程
2023-08-01 14:58:01
Python3.6安装及引入Requests库的实现方法
2022-06-12 16:55:46
![](https://img.aspxhome.com/file/2023/4/120644_0s.jpg)
Oracle数据加载和卸载的实现方法
2024-01-27 20:19:08
Windows 安装 Anaconda3+PyCharm的方法步骤
2023-05-21 07:40:53
![](https://img.aspxhome.com/file/2023/8/107998_0s.png)
Mootools 1.2教程(22)——同时进行多个形变动画
2008-12-29 14:11:00
巧用Javascript的逻辑运算符
2024-04-16 09:47:42
Ajax request response 乱码解决方法
2024-06-05 09:21:52
python3 简单实现组合设计模式
2023-06-12 19:15:50
![](https://img.aspxhome.com/file/2023/2/59482_0s.png)
VS2015安装之后加装Sql server2014详细步骤
2024-01-18 04:39:56
关于Python中浮点数精度处理的技巧总结
2021-12-28 15:12:53
![](https://img.aspxhome.com/file/2023/9/79589_0s.jpg)
python Jupyter运行时间实例过程解析
2022-04-15 04:38:21
Python正则表达式指南 推荐
2021-10-18 22:04:30
![](https://img.aspxhome.com/file/2023/1/125211_0s.png)
关于浏览器的一些观点
2008-08-06 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20088/6/200886125027461s.gif)
python爬虫实现爬取同一个网站的多页数据的实例讲解
2021-04-11 13:52:09