欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php mysql數(shù)據(jù)庫(kù)操作類(lèi)(實(shí)例講解)

 更新時(shí)間:2017年08月06日 17:07:05   作者:李盛鵬  
本白演示的代碼屬于較為簡(jiǎn)單的數(shù)據(jù)庫(kù)封裝類(lèi),較適合初學(xué),需要的朋友可以參考下

接著稍微說(shuō)說(shuō)整體的思路。整個(gè)類(lèi)的封裝,包含一個(gè)連接數(shù)據(jù)庫(kù)的私有屬性$conn和若干操作函數(shù)。$conn在對(duì)象實(shí)例化的時(shí)候,由構(gòu)造函數(shù)處理傳入的參數(shù)后返回一個(gè)資源型的連接句柄。而后即可通過(guò)調(diào)用該實(shí)例化的對(duì)象的相應(yīng)方法對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪查改的操作。

talk less and show code:

<?php
/** 
*以下代碼用于數(shù)據(jù)庫(kù)操作類(lèi)的封裝
* 
* @author rex<rex.sp.li@aliyun.com> 
* @version 1.0
* @since 2015
*/


class Mysql{

//數(shù)據(jù)庫(kù)連接返回值
private $conn;

/**
* [構(gòu)造函數(shù),返回值給$conn]
* @param [string] $hostname [主機(jī)名]
* @param [string] $username[用戶(hù)名]
* @param [string] $password[密碼]
* @param [string] $dbname[數(shù)據(jù)庫(kù)名]
* @param [string] $charset[字符集]
* @return [null]

*/

function __construct($hostname,$username,$password,$dbname,$charset='utf8'){
  $conn = @mysql_connect($hostname,$username,$password);
  if(!$conn){
    echo '連接失敗,請(qǐng)聯(lián)系管理員';
    exit;
  }
  $this->conn = $conn;
  $res = mysql_select_db($dbname);
  if(!$res){
  echo '連接失敗,請(qǐng)聯(lián)系管理員';
  exit;
  }
  mysql_set_charset($charset);
}
function __destruct(){
  mysql_close();
}
/**
* [getAll 獲取所有信息]
* @param [string] $sql [sql語(yǔ)句]
* @return [array] [返回二維數(shù)組]
*/
function getAll($sql){
  $result = mysql_query($sql,$this->conn);
  $data = array();
  if($result && mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){
    $data[] = $row;
    }
  }
  return $data;
}
/**
* [getOne 獲取單條數(shù)據(jù)]
* @param [string] $sql [sql語(yǔ)句]
* @return [array] [返回一維數(shù)組]
*/
function getOne($sql){
  $result = mysql_query($sql,$this->conn);
  $data = array();
  if($result && mysql_num_rows($result)>0){
    $data = mysql_fetch_assoc($result);
  }
  return $data;
}

/**
* [getOne 獲取單條數(shù)據(jù)]
* @param [string] $table [表名]
* @param [string] $data [由字段名當(dāng)鍵,屬性當(dāng)鍵值的一維數(shù)組]
* @return [type] [返回false或者插入數(shù)據(jù)的id]
*/

function insert($table,$data){
  $str = '';
  $str .="INSERT INTO `$table` ";
  $str .="(`".implode("`,`",array_keys($data))."`) "; 
  $str .=" VALUES ";
  $str .= "('".implode("','",$data)."')";
  $res = mysql_query($str,$this->conn);
  if($res && mysql_affected_rows()>0){
      return mysql_insert_id();
  }else{
    return false;
  }
}
/**
* [update 更新數(shù)據(jù)庫(kù)]
* @param [string] $table [表名]
* @param [array] $data [更新的數(shù)據(jù),由字段名當(dāng)鍵,屬性當(dāng)鍵值的一維數(shù)組]
* @param [string] $where [條件,‘字段名'=‘字段屬性']
* @return [type] [更新成功返回影響的行數(shù),更新失敗返回false]
*/
function update($table,$data,$where){
  $sql = 'UPDATE '.$table.' SET ';
  foreach($data as $key => $value){
  $sql .= "`{$key}`='{$value}',";
  }
  $sql = rtrim($sql,',');
  $sql .= " WHERE $where";
  $res = mysql_query($sql,$this->conn);
  if($res && mysql_affected_rows()){
    return mysql_affected_rows();
  }else{
  return false;
  }
}

/**
* [delete 刪除數(shù)據(jù)]
* @param [string] $table [表名]
* @param [string] $where [條件,‘字段名'=‘字段屬性']
* @return [type] [成功返回影響的行數(shù),失敗返回false]
*/
function del($table,$where){
  $sql = "DELETE FROM `{$table}` WHERE {$where}";
  $res = mysql_query($sql,$this->conn);
  if($res && mysql_affected_rows()){
    return mysql_affected_rows();
  }else{
  return false;
  }
}
}

實(shí)例化類(lèi):

<?php

//包含數(shù)據(jù)庫(kù)操作類(lèi)文件
include 'mysql.class.php';

//設(shè)置傳入?yún)?shù)
$hostname='localhost';
$username='root';
$password='123456';
$dbname='aisi';
$charset = 'utf8';

//實(shí)例化對(duì)象

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

//獲取一條數(shù)據(jù)

$sql = "SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1";
$count = $db->getOne($sql);

//獲取多條數(shù)據(jù)

$sql = "SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";
$service = $db->getAll($sql);

//插入數(shù)據(jù)

$arr = array(
'as_article_title'=>'數(shù)據(jù)庫(kù)操作類(lèi)',
'as_article_author'=>'rex',
);
$res = $db->insert('as_article',$arr);

//更新數(shù)據(jù)

$arr = array(
'as_article_title'=>'實(shí)例化對(duì)象',
'as_article_author'=>'Lee',
);
$where = "as_article_id=1";
$res = $db->update('as_article',$arr,$where);

//刪除數(shù)據(jù)

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

?>

演示完代碼,大概說(shuō)幾句。

  getOne方法傳入$sql的sql語(yǔ)句用于查詢(xún)單條數(shù)據(jù)并返回一維數(shù)組;getAll方法同樣傳入sql語(yǔ)句,用于查詢(xún)多條數(shù)據(jù),并返回二維數(shù)組;insert方法傳入表名和關(guān)聯(lián)數(shù)組,返回boolen型或者插入數(shù)據(jù)對(duì)應(yīng)索引;update方法傳入表名、關(guān)聯(lián)數(shù)組和條件,返回boolen或者影響的行數(shù);del方法傳入表名和條件,返回boolen型。

  that's all,but not the all.有興趣的朋友可以把getOne和getAll直接傳入sql語(yǔ)句作為參數(shù)的方式再優(yōu)化一下。

相關(guān)文章

  • PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例

    PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例

    這篇文章主要介紹了PHP設(shè)計(jì)模式之適配器模式代碼實(shí)例,本文講解了目標(biāo)、角色、應(yīng)用場(chǎng)景、優(yōu)勢(shì)等內(nèi)容,并給出代碼實(shí)例,需要的朋友可以參考下
    2015-05-05
  • PHP會(huì)話(huà)控制:Session與Cookie詳解

    PHP會(huì)話(huà)控制:Session與Cookie詳解

    這篇文章主要介紹了PHP會(huì)話(huà)控制:Session與Cookie詳解,本文詳細(xì)講解了PHP中Session與Cookie的相關(guān)知識(shí),涵蓋面較廣,需要的朋友可以參考下
    2014-09-09
  • php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解

    php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解

    這篇文章主要介紹了php檢查函數(shù)必傳參數(shù)是否存在的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • 詳解PHP中cookie和session的區(qū)別及cookie和session用法小結(jié)

    詳解PHP中cookie和session的區(qū)別及cookie和session用法小結(jié)

    這篇文章主要介紹了PHP中cookie和session的區(qū)別及cookie和session用法小結(jié)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Thinkphp5框架簡(jiǎn)單實(shí)現(xiàn)鉤子(Hook)行為的方法示例

    Thinkphp5框架簡(jiǎn)單實(shí)現(xiàn)鉤子(Hook)行為的方法示例

    這篇文章主要介紹了Thinkphp5框架簡(jiǎn)單實(shí)現(xiàn)鉤子(Hook)行為的方法,結(jié)合實(shí)例形式詳細(xì)分析了Thinkphp5框架實(shí)現(xiàn)鉤子(Hook)行為的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • Laravel程序架構(gòu)設(shè)計(jì)思路之使用動(dòng)作類(lèi)

    Laravel程序架構(gòu)設(shè)計(jì)思路之使用動(dòng)作類(lèi)

    這篇文章主要給大家介紹了關(guān)于Laravel程序架構(gòu)設(shè)計(jì)思路之使用動(dòng)作類(lèi)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢(xún)的方法

    tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢(xún)的方法

    這篇文章主要介紹了tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢(xún)的方法,結(jié)合實(shí)例形式分析了thinkPHP5框架多數(shù)據(jù)庫(kù)查詢(xún)的相關(guān)配置、初始化及調(diào)用相關(guān)操作技巧,需要的朋友可以參考下
    2019-01-01
  • Windows下Apache + PHP SESSION丟失的解決過(guò)程全紀(jì)錄

    Windows下Apache + PHP SESSION丟失的解決過(guò)程全紀(jì)錄

    這篇文章主要介紹了Windows下Apache + PHP SESSION丟失的解決過(guò)程全紀(jì)錄,花費(fèi)了很長(zhǎng)時(shí)間,最終解決的方式卻令人啼笑皆非,郁悶之極。
    2015-04-04
  • ThinkPHP5.1表單令牌Token失效問(wèn)題的解決

    ThinkPHP5.1表單令牌Token失效問(wèn)題的解決

    這篇文章主要給大家介紹了關(guān)于ThinkPHP5.1表單令牌Token失效問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ThinkPHP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • PHP SplObjectStorage使用實(shí)例

    PHP SplObjectStorage使用實(shí)例

    這篇文章主要介紹了PHP SplObjectStorage使用實(shí)例,SplObjectStorage是SPL標(biāo)準(zhǔn)庫(kù)中的數(shù)據(jù)結(jié)構(gòu)對(duì)象容器,用來(lái)存儲(chǔ)一組對(duì)象,特別是當(dāng)你需要唯一標(biāo)識(shí)對(duì)象的時(shí)候,需要的朋友可以參考下
    2015-05-05

最新評(píng)論