復(fù)制代碼

創(chuàng)新互聯(lián)建站專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、羅湖網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場景定制、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為羅湖等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
代碼如下:
?php
/*
MYSQL
數(shù)據(jù)庫訪問封裝類
MYSQL
數(shù)據(jù)訪問方式,php4支持以mysql_開頭的過程訪問方式,php5開始支持以mysqli_開頭的過程和mysqli面向?qū)ο?/p>
訪問方式,本封裝類以mysql_封裝
數(shù)據(jù)訪問的一般流程:
1,連接數(shù)據(jù)庫
mysql_connect
or
mysql_pconnect
2,選擇數(shù)據(jù)庫
mysql_select_db
3,執(zhí)行SQL查詢
mysql_query
4,處理返回的數(shù)據(jù)
mysql_fetch_array
mysql_num_rows
mysql_fetch_assoc
mysql_fetch_row
etc
*/
class
db_mysql
{
var
$querynum
=
;
//當(dāng)前頁面進程查詢數(shù)據(jù)庫的次數(shù)
var
$dblink
;
//數(shù)據(jù)庫連接資源
//鏈接數(shù)據(jù)庫
function
connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0
,
$halt=true)
{
$func
=
empty($pconnect)
?
'mysql_connect'
:
'mysql_pconnect'
;
$this-dblink
=
@$func($dbhost,$dbuser,$dbpw)
;
if
($halt
!$this-dblink)
{
$this-halt("無法鏈接數(shù)據(jù)庫!");
}
//設(shè)置查詢字符集
mysql_query("SET
character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this-dblink)
;
//選擇數(shù)據(jù)庫
$dbname
@mysql_select_db($dbname,$this-dblink)
;
}
//選擇數(shù)據(jù)庫
function
select_db($dbname)
{
return
mysql_select_db($dbname,$this-dblink);
}
//執(zhí)行SQL查詢
function
query($sql)
{
$this-querynum++
;
return
mysql_query($sql,$this-dblink)
;
}
//返回最近一次與連接句柄關(guān)聯(lián)的INSERT,UPDATE
或DELETE
查詢所影響的記錄行數(shù)
function
affected_rows()
{
return
mysql_affected_rows($this-dblink)
;
}
//取得結(jié)果集中行的數(shù)目,只對select查詢的結(jié)果集有效
function
num_rows($result)
{
return
mysql_num_rows($result)
;
}
//獲得單格的查詢結(jié)果
function
result($result,$row=0)
{
return
mysql_result($result,$row)
;
}
//取得上一步
INSERT
操作產(chǎn)生的
ID,只對表有AUTO_INCREMENT
ID的操作有效
function
insert_id()
{
return
($id
=
mysql_insert_id($this-dblink))
=
?
$id
:
$this-result($this-query("SELECT
last_insert_id()"),
0);
}
//從結(jié)果集提取當(dāng)前行,以數(shù)字為key表示的關(guān)聯(lián)數(shù)組形式返回
function
fetch_row($result)
{
return
mysql_fetch_row($result)
;
}
//從結(jié)果集提取當(dāng)前行,以字段名為key表示的關(guān)聯(lián)數(shù)組形式返回
function
fetch_assoc($result)
{
return
mysql_fetch_assoc($result);
}
//從結(jié)果集提取當(dāng)前行,以字段名和數(shù)字為key表示的關(guān)聯(lián)數(shù)組形式返回
function
fetch_array($result)
{
return
mysql_fetch_array($result);
}
//關(guān)閉鏈接
function
close()
{
return
mysql_close($this-dblink)
;
}
//輸出簡單的錯誤html提示信息并終止程序
function
halt($msg)
{
$message
=
"html\nhead\n"
;
$message
.=
"meta
content='text/html;charset=gb2312'\n"
;
$message
.=
"/head\n"
;
$message
.=
"body\n"
;
$message
.=
"數(shù)據(jù)庫出錯:".htmlspecialchars($msg)."\n"
;
$message
.=
"/body\n"
;
$message
.=
"/html"
;
echo
$message
;
exit
;
}
}
?
封裝是php面向?qū)ο蟮钠渲幸粋€特性,將多個可重復(fù)使用的函數(shù)封裝到一個類里面。在使用時直接實例化該類的某一個方法,獲得需要的數(shù)據(jù)
如果是私有的方法和屬性值,外部無法訪問,具有一定的保護作用。
面向?qū)ο笾庋b 例子
class A{
public $name = '老王';
// protected $name = '老王';
//private $name = '老王';
//自己訪問
public function saya(){
return $this-name;
}
}
//實例化對象
$b = new A;
//public:外部、家族、自己都可以訪問
//protected:家族和自己都可以訪問,外部無法訪問
//private:自己可以訪問,外部和家族都無法訪問
echo '外部訪問:'.$b-name.'br'; //如果是私有的,訪問不了
echo '家族訪問:'.$b-sayb().'br';
echo '自己訪問:'.$b-saya().'br';
php封裝mysql類
復(fù)制代碼
代碼如下:
?php
class
Mysql
{
private
$host;
private
$user;
private
$pwd;
private
$dbName;
private
$charset;
private
$conn
=
null;
public
function
__construct()
{
$this-host
=
'localhost';
$this-user
=
'root';
$this-pwd
=
'root';
$this-dbName
=
'test';
$this-connect($this-host,$this-user,$this-pwd);
$this-switchDb($this-dbName);
$this-setChar($this-charset);
}
//負(fù)責(zé)鏈接
private
function
connect($h,$u,$p)
{
$conn
=
mysql_connect($h,$u,$p);
$this-conn
=
$conn;
}
//負(fù)責(zé)切換數(shù)據(jù)庫
public
function
switchDb($db)
{
$sql
=
'use'
.
$db;
$this-query($sql);
}
//負(fù)責(zé)設(shè)置字符集
public
function
setChar($char)
{
$sql
=
'set
names'
.
$char;
$this-query($sql);
}
//負(fù)責(zé)發(fā)送sql查詢
public
function
query($sql)
{
return
mysql_query($sql,$this-conn);
}
//負(fù)責(zé)獲取多行多列的select結(jié)果
public
function
getAll($sql)
{
$list
=
array();
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
while
($row
=
mysql_fetch_assoc($rs))
{
$list[]
=
$row;
}
return
$list;
}
public
function
getRow($sql)
{
$rs
=
$this-query($sql);
if(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
}
public
function
getOne($sql)
{
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
return
$row[0];
}
public
function
close()
{
mysql_close($this-conn);
}
}
echo
'pre';
$mysql
=
new
Mysql();
print_r($mysql);
$sql
=
"insert
into
stu
values
(4,'wangwu','99998')";
if($mysql-query($sql)){
echo
"query成功";
}else
{
echo
"失敗";
}
echo
"br
/";
$sql
=
"select
*
from
stu";
$arr
=
$mysql-getAll($sql);
print_r($arr);
?
?php
class MySQL{
private $host; //服務(wù)器地址
private $name; //登錄賬號
private $pwd; //登錄密碼
private $dBase; //數(shù)據(jù)庫名稱
private $conn; //數(shù)據(jù)庫鏈接資源
private $result; //結(jié)果集
private $msg; //返回結(jié)果
private $fields; //返回字段
private $fieldsNum; //返回字段數(shù)
private $rowsNum; //返回結(jié)果數(shù)
private $rowsRst; //返回單條記錄的字段數(shù)組
private $filesArray = array(); //返回字段數(shù)組
private $rowsArray = array(); //返回結(jié)果數(shù)組
private $charset='utf8'; //設(shè)置操作的字符集
private $query_count=0; //查詢結(jié)果次數(shù)
static private $_instance; //存儲對象
//初始化類
private function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '') $this-host = $host;
if($name != '') $this-name = $name;
if($pwd != '') $this-pwd = $pwd;
if($dBase != '') $this-dBase = $dBase;
$this-init_conn();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this-$name=$value;
}
public function __get($name){
return $this-$name;
}
//鏈接數(shù)據(jù)庫
function init_conn(){
$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die('connect db fail !');
@mysql_select_db($this-dBase,$this-conn) or die('select db fail !');
mysql_query("set names ".$this-charset);
}
//查詢結(jié)果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數(shù)
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結(jié)果數(shù)
function getRowsNum($sql){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
}else{
return '';
}
}
//取得記錄數(shù)組(單條記錄)
function getRowsRst($sql,$type=MYSQL_BOTH){
$this-mysql_query_rst($sql);
if(empty($this-result)) return '';
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
}else{
return '';
}
}
//取得記錄數(shù)組(多條記錄)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : '';
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
}
return $this-rowsArray;
}else{
return '';
}
}
//更新、刪除、添加記錄數(shù)
function uidRst($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
$this-rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this-rowsNum;
}else{
return '';
}
}
//返回最近插入的一條數(shù)據(jù)庫的id值
function returnRstId($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//獲取對應(yīng)的字段值
function getFields($sql,$fields){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this-result) 0){
$tmpfld = @mysql_fetch_row($this-result);
$this-fields = $tmpfld[$fields];
}
return $this-fields;
}else{
return '';
}
}
//錯誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結(jié)果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
}
//關(guān)閉數(shù)據(jù)庫
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
}
//取得數(shù)據(jù)庫版本
function db_version() {
return mysql_get_server_info();
}
}
-子類只能繼承父類的非私有屬性
-子類繼承父類后,相當(dāng)于將父類的屬性和方法copy到子類,可以直接使用$this調(diào)用該屬性;
-php只能單繼承,不支持一個類繼承多個類。但是一個類可以進行多層繼承
類實現(xiàn)封裝是為了不讓外面的類隨意修改一個類的成員變量,所以在定義一個類的成員的時候,我們使用private關(guān)鍵字設(shè)置這個成員的訪問權(quán)限只能被這個類的其他成員方法調(diào)用,而不能被其他類中的方法調(diào)用,即通過本類中提供的方法來訪問本類中的私有屬性
-所以在該類中我們會提供一個訪問私有屬性的方法
-然后我們一般會定義兩個方法來實現(xiàn)對一個變量的操作,即__get()和__set()方法
一個類被多個子類繼承,如果這個類的某個方法,在多個子類中,表現(xiàn)出不同的功能,我們稱這種行為為多態(tài)(同一個類的不同子類表現(xiàn)出不同的形態(tài))
-子類繼承父類-子類重寫父類的方法-父類引用指向子類對象
本文名稱:php數(shù)據(jù)封裝類,PHP封裝
分享路徑:http://chinadenli.net/article39/dsedpsh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、App開發(fā)、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)、網(wǎng)站制作、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)