一個php Mysql類 可以參考學(xué)習(xí)熟悉下
更新時(shí)間:2009年06月21日 18:46:29 作者:
慢慢研究吧,非常適合學(xué)習(xí)的php數(shù)據(jù)庫(mysql)類,也可以拿來直接就用,稍微熟悉一下就可以啦!
復(fù)制代碼 代碼如下:
<?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基于單例模式實(shí)現(xiàn)的mysql類
- php實(shí)現(xiàn)帶讀寫分離功能的MySQL類完整實(shí)例
- php封裝的連接Mysql類及用法分析
- 十二個常見的PHP+MySql類免費(fèi)CMS系統(tǒng)
- php mysql數(shù)據(jù)庫操作類
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫備份類
- mysql+php分頁類(已測)
- PHP+Mysql樹型結(jié)構(gòu)(無限分類)數(shù)據(jù)庫設(shè)計(jì)的2種方式實(shí)例
- PHP訪問MYSQL數(shù)據(jù)庫封裝類(附函數(shù)說明)
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫操作類分享
- PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫操作類
- php基于單例模式封裝mysql類完整實(shí)例
相關(guān)文章
thinkPHP5框架閉包函數(shù)與子查詢傳參用法示例
這篇文章主要介紹了thinkPHP5框架閉包函數(shù)與子查詢傳參用法,結(jié)合實(shí)例形式分析了thinkPHP5閉包查詢與參數(shù)傳遞相關(guān)操作技巧,需要的朋友可以參考下2018-08-08ThinkPHP 3.2.3實(shí)現(xiàn)頁面靜態(tài)化功能的方法詳解
頁面靜態(tài)化是我們在開發(fā)網(wǎng)站的時(shí)候經(jīng)常需要的一個功能,下面這篇文章主要給大家介紹了關(guān)于ThinkPHP 3.2.3實(shí)現(xiàn)頁面靜態(tài)化功能的方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08PHP開發(fā)框架Laravel數(shù)據(jù)庫操作方法總結(jié)
這篇文章主要介紹了PHP開發(fā)框架Laravel數(shù)據(jù)庫操作方法總結(jié),包含Select查詢、Insert語句、update語句、Delete語句、事務(wù)等,需要的朋友可以參考下2014-09-09thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能
這篇文章主要介紹了thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了修改QQ登陸接口并整合進(jìn)thinkPHP5項(xiàng)目中的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10