php mysql資料庫操作類 例項講解

2022-10-05 00:48:20 字數 3094 閱讀 6024

接著稍微說說整體的思路。整個類的封裝,包含乙個連線資料庫的私有屬性$conn和若干操作函式。$conn在物件例項化的時候,由建構函式處理傳入的引數後返回乙個資源型的連線控制代碼。而後即可通過呼叫該例項化的物件的相應方法對資料庫進行增刪查改的操作。

talk less and show code:

php/**

*以下**用於資料庫操作類的封裝

* * @author rex

* @version 1.0

* @since 2015

*/class mysql

$this->conn = $conn;

$res = mysql_select_db($dbname);

if(!$res)

mysql_set_charset($charset);

}function __destruct()

/*** [getall 獲取所有資訊]

* @param [string] $sql [sql語句]

* @return [array] [返回二維陣列]

*/function getall($sql)

}  return $data;

}/**

* [getone 獲取單條資料]

* @param [string] $sql [sql語句]

* @return [array] [返回一維陣列]

*/function getone($sql)

return $data;

}/**

* [getone 獲取單條資料]

* @param [st $table [表名]

* @param [string] $data [由欄位名當鍵,屬性當鍵值的一維陣列]

* @return [type] [返回false或者插入資料的id]

*/function insert($table,$data)else

}/**

* [update 更新資料庫]

* @param [string] $table [表名]

* @param [array] $data [更新的資料,由欄位名當鍵,屬性當鍵值的一維陣列]

* @param [string] $where [條件,『欄位名'=『字段屬性']

* @return [type] [更新成功返回影響的行數,更新失敗返回false]

*/function update($table,$data,$where)`='',";

}  $sql = rtrim($sql,',');

$sql .= " where $where";

$res = mysql_query($sql,$this->conn);

if($res && mysql_affected_rows())else

}/**

* [delete 刪除資料]

* @param [string] $table [表名]

* @param [string] $where [條件,『欄位名'=『字段屬性']

* @return [type] [成功返回影響的行數,失敗返回false]

*/function del($table,$where)` where ";

$res = mysql_query>conn);

if($res && mysql_affected_rows())else

}}例項化類:

<?php //包含資料庫操作類檔案

include 'mysql.class.php';

//設定傳入引數

$hostname='localhost';

$username='root';

$password='123456';

$dbname='aisi';

$charset = 'utf8';

//例項化物件

$db = new mysql($hostname,$username,$password,$dbname);

//獲取一條資料

$sql = "select count(as_article_id) as count from as_article where as_article_type_id=1";

$count = $db->getone($sql);

//獲取多條資料

$sql = "select * from as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";

$service = $db->getall($sql);

//插入資料

$arr = array(

'as_article_title'=>'資料庫操作類',

'as_article_author'=>'rex',

);$res = $db->insert('as_article',$arr);

//更新資料

$arr = array(

'as_article_title'=>'例項化物件',

'as_article_author'=>'lee',

);$where = "as_article_id=1";

$res = $db->update('as_article',$arr,$where);

//刪除資料

$where = "as_article_id=1";

$res = $db->del('as_article',$where);

?>

演示完**,大概說幾句。

getone方法傳入$sql的sql語句用於查詢單條資料並返回一維陣列;getall方法同樣傳入sql語句,用於查詢多條資料,並返回二維陣列;insert方法傳入表名和關聯陣列,返回boolen型或者插入資料對應索引;update方法傳入表名、關聯陣列和條件,返回boolen或者影響的行數;del方法傳入表名和條件,返回boolen型。

that's all,but not the all.有興趣的朋友可以把getone和getall直接傳入sql語句作為引數的方式再優化一下。

本文標題: php mysql資料庫操作類(例項講解)

本文位址:

php mysql資料庫操作類演示

設計目標 1,該類一例項化,就可以自動連線上mysql資料庫 2,該類可以單獨去設定要使用的連線編碼 set names 3,該類可以單獨去設定要使用的資料庫 use 4,可以主動關閉連線 設計乙個類 mysql資料庫操作類 設計目標 1,該類一例項化,就可以自動連線上mysql資料庫 2,該類可以...

PHP Mysql資料庫備份類

使用方法 require once backdata.class.php link mysql connect localhost 資料庫名 密碼 or die could not connect to server.mysql query use cms link mysql query set ...

php mysql 資料庫鏈結與操作

做開發時,經常會與資料庫打交道,記錄一下php與mysql建立鏈結的方法 1.2.class mysql 24.25.資料庫表查詢 26.如 select from table 27.28.function getallinfo table 38.num 0 39.data array 40.將查詢...