一個(gè)php Mysql類 可以參考學(xué)習(xí)熟悉下
更新時(shí)間:2009年06月21日 18:46:29 作者:
慢慢研究吧,非常適合學(xué)習(xí)的php數(shù)據(jù)庫(kù)(mysql)類,也可以拿來(lái)直接就用,稍微熟悉一下就可以啦!
復(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類及用法分析
- 十二個(gè)常見(jiàn)的PHP+MySql類免費(fèi)CMS系統(tǒng)
- php mysql數(shù)據(jù)庫(kù)操作類
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)備份類
- mysql+php分頁(yè)類(已測(cè))
- PHP+Mysql樹(shù)型結(jié)構(gòu)(無(wú)限分類)數(shù)據(jù)庫(kù)設(shè)計(jì)的2種方式實(shí)例
- PHP訪問(wèn)MYSQL數(shù)據(jù)庫(kù)封裝類(附函數(shù)說(shuō)明)
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)操作類分享
- PHP實(shí)現(xiàn)PDO的mysql數(shù)據(jù)庫(kù)操作類
- php基于單例模式封裝mysql類完整實(shí)例
相關(guān)文章
php curl模擬post請(qǐng)求和提交多維數(shù)組的示例代碼
這篇文章主要介紹了php curl模擬post請(qǐng)求和提交多維數(shù)組的示例代碼,需要的朋友可以參考下2015-11-11
ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作示例
這篇文章主要介紹了ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作,結(jié)合實(shí)例形式分析了thinkPHP5.1框架數(shù)據(jù)庫(kù)連接的常用方式與針對(duì)數(shù)據(jù)庫(kù)增刪改查操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
PHP中模糊查詢并關(guān)聯(lián)三個(gè)select框
這篇文章主要介紹了PHP中模糊查詢并關(guān)聯(lián)三個(gè)select框,需要的朋友可以參考下2017-06-06
用php和jQuery來(lái)實(shí)現(xiàn)“頂”和“踩”的投票功能
本文結(jié)合實(shí)例,講解使用PHP+MySql+jQuery實(shí)現(xiàn)的“頂”和“踩”投票功能,判斷用戶的投票行為是否有效。2016-10-10
PHP面向?qū)ο缶幊讨钊肜斫夥椒ㄖ剌d與方法覆蓋(多態(tài))
這篇文章主要介紹了PHP面向?qū)ο缶幊讨钊肜斫夥椒ㄖ剌d與方法覆蓋(多態(tài))的相關(guān)資料,需要的朋友可以參考下2015-12-12
laravel實(shí)現(xiàn)上傳圖片,并且制作縮略圖,按照日期存放的代碼
今天小編就為大家分享一篇laravel實(shí)現(xiàn)上傳圖片,并且制作縮略圖,按照日期存放的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

