您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)php數(shù)據(jù)庫(kù)操作類代碼,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
數(shù)據(jù)庫(kù)操縱基本流程為:
1、連接數(shù)據(jù)庫(kù)服務(wù)器
2、選擇數(shù)據(jù)庫(kù)
3、執(zhí)行SQL語(yǔ)句
4、處理結(jié)果集
5、打印操作信息
其中用到的相關(guān)函數(shù)有
?resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) 連接數(shù)據(jù)庫(kù)服務(wù)器
?resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] ) 連接數(shù)據(jù)庫(kù)服務(wù)器,長(zhǎng)連接
?int mysql_affected_rows ( [resource link_identifier] )取得最近一次與 link_identifier 關(guān)聯(lián)的 INSERT,UPDATE 或 DELETE 查詢所影響的記錄行數(shù)。
?bool mysql_close ( [resource link_identifier] )如果成功則返回 TRUE,失敗則返回 FALSE。
?int mysql_errno ( [resource link_identifier] )返回上一個(gè) MySQL 函數(shù)的錯(cuò)誤號(hào)碼,如果沒(méi)有出錯(cuò)則返回 0(零)。
?string mysql_error ( [resource link_identifier] )返回上一個(gè) MySQL 函數(shù)的錯(cuò)誤文本,如果沒(méi)有出錯(cuò)則返回 ''(空字符串)。如果沒(méi)有指定連接資源號(hào),則使用上一個(gè)成功打開(kāi)的連接從 MySQL 服務(wù)器提取錯(cuò)誤信息。
?array mysql_fetch_array ( resource result [, int result_type] )返回根據(jù)從結(jié)果集取得的行生成的數(shù)組,如果沒(méi)有更多行則返回 FALSE。
?bool mysql_free_result ( resource result )釋放所有與結(jié)果標(biāo)識(shí)符 result 所關(guān)聯(lián)的內(nèi)存。
?int mysql_num_fields ( resource result )返回結(jié)果集中字段的數(shù)目。
?int mysql_num_rows ( resource result )返回結(jié)果集中行的數(shù)目。此命令僅對(duì) SELECT 語(yǔ)句有效。要取得被 INSERT,UPDATE 或者 DELETE 查詢所影響到的行的數(shù)目,用 mysql_affected_rows()。
?resource mysql_query ( string query [, resource link_identifier] ) 向與指定的連接標(biāo)識(shí)符關(guān)聯(lián)的服務(wù)器中的當(dāng)前活動(dòng)數(shù)據(jù)庫(kù)發(fā)送一條查詢。如果沒(méi)有指定 link_identifier,則使用上一個(gè)打開(kāi)的連接。如果沒(méi)有打開(kāi)的連接,本函數(shù)會(huì)嘗試無(wú)參數(shù)調(diào)用 mysql_connect() 函數(shù)來(lái)建立一個(gè)連接并使用之。查詢結(jié)果會(huì)被緩存
代碼如下:
復(fù)制代碼 代碼如下:
class mysql {
private $db_host; //數(shù)據(jù)庫(kù)主機(jī)
private $db_user; //數(shù)據(jù)庫(kù)登陸名
private $db_pwd; //數(shù)據(jù)庫(kù)登陸密碼
private $db_name; //數(shù)據(jù)庫(kù)名
private $db_charset; //數(shù)據(jù)庫(kù)字符編碼
private $db_pconn; //長(zhǎng)連接標(biāo)識(shí)位
private $debug; //調(diào)試開(kāi)啟
private $conn; //數(shù)據(jù)庫(kù)連接標(biāo)識(shí)
private $msg = ""; //數(shù)據(jù)庫(kù)操縱信息
// private $sql = ""; //待執(zhí)行的SQL語(yǔ)句
public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_name = $db_name;
$this->db_charset = $db_chaeset;
$this->db_pconn = $db_pconn;
$this->result = '';
$this->debug = $debug;
$this->initConnect();
}
public function initConnect() {
if ($this->db_pconn) {
$this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
$this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}
if ($this->conn) {
$this->query("SET NAMES " . $this->db_charset);
} else {
$this->msg = "數(shù)據(jù)庫(kù)連接出錯(cuò),錯(cuò)誤編號(hào):" . mysql_errno() . "錯(cuò)誤原因:" . mysql_error();
}
$this->selectDb($this->db_name);
}
public function selectDb($dbname) {
if ($dbname == "") {
$this->db_name = $dbname;
}
if (!mysql_select_db($this->db_name, $this->conn)) {
$this->msg = "數(shù)據(jù)庫(kù)不可用";
}
}
public function query($sql, $debug = false) {
if (!$debug) {
$this->result = @mysql_query($sql, $this->conn);
} else {
}
if ($this->result == false) {
$this->msg = "sql執(zhí)行出錯(cuò),錯(cuò)誤編號(hào):" . mysql_errno() . "錯(cuò)誤原因:" . mysql_error();
}
// var_dump($this->result);
}
public function select($tableName, $columnName = "*", $where = "") {
$sql = "SELECT " . $columnName . " FROM " . $tableName;
$sql .= $where ? " WHERE " . $where : null;
$this->query($sql);
}
public function findAll($tableName) {
$sql = "SELECT * FROM $tableName";
$this->query($sql);
}
public function insert($tableName, $column = array()) {
$columnName = "";
$columnValue = "";
foreach ($column as $key => $value) {
$columnName .= $key . ",";
$columnValue .= "'" . $value . "',";
}
$columnName = substr($columnName, 0, strlen($columnName) - 1);
$columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
$sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";
$this->query($sql);
if($this->result){
$this->msg = "數(shù)據(jù)插入成功。新插入的id為:" . mysql_insert_id($this->conn);
}
}
public function update($tableName, $column = array(), $where = "") {
$updateValue = "";
foreach ($column as $key => $value) {
$updateValue .= $key . "='" . $value . "',";
}
$updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
$sql = "UPDATE $tableName SET $updateValue";
$sql .= $where ? " WHERE $where" : null;
$this->query($sql);
if($this->result){
$this->msg = "數(shù)據(jù)更新成功。受影響行數(shù):" . mysql_affected_rows($this->conn);
}
}
public function delete($tableName, $where = ""){
$sql = "DELETE FROM $tableName";
$sql .= $where ? " WHERE $where" : null;
$this->query($sql);
if($this->result){
$this->msg = "數(shù)據(jù)刪除成功。受影響行數(shù):" . mysql_affected_rows($this->conn);
}
}
public function fetchArray($result_type = MYSQL_BOTH){
$resultArray = array();
$i = 0;
while($result = mysql_fetch_array($this->result, $result_type)){
$resultArray[$i] = $result;
$i++;
}
return $resultArray;
}
// public function fetchObject(){
// return mysql_fetch_object($this->result);
// }
public function printMessage(){
return $this->msg;
}
public function freeResult(){
@mysql_free_result($this->result);
}
public function __destruct() {
if(!empty($this->result)){
$this->freeResult();
}
mysql_close($this->conn);
}
}
調(diào)用代碼如下
復(fù)制代碼 代碼如下:
require_once 'mysql_V1.class.php';
require_once 'commonFun.php';
$db = new mysql('localhost', 'root', '', "test");
//select 查
$db->select("user", "*", "username = 'system'");
$result = $db->fetchArray(MYSQL_ASSOC);
print_r($result);
dump($db->printMessage());
//insert 增
//$userInfo = array('username'=>'system', 'password' => md5("system"));
//$db->insert("user", $userInfo);
//dump($db->printMessage());
//update 改
//$userInfo = array('password' => md5("123456"));
//$db->update("user", $userInfo, "id = 2");
//dump($db->printMessage());
//delete 刪
//$db->delete("user", "id = 1");
//dump($db->printMessage());
//findAll 查詢?nèi)?br/> $db->findAll("user");
$result = $db->fetchArray();
dump($result);
ps,個(gè)人比較喜歡tp的dump函數(shù),所以在commonFun.php文件中拷貝了友好打印函數(shù)。使用時(shí)將其改為print_r()即可。
關(guān)于“如何實(shí)現(xiàn)php數(shù)據(jù)庫(kù)操作類代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。