PHP DB 資料庫連線類

2021-09-13 19:31:16 字數 4339 閱讀 3568

<

?php

// 資料庫連線類

class

db//連線資料庫

private

function

db_connect()

}//設定字符集

private

function

db_charset()

");}

//選擇資料庫

private

function

db_usedb()

");}

//私有的轉殖

private

function

__clone()

//公用的靜態方法

public

static

function

getintance()

return self:

:$dbcon;

}//執行sql語句的方法

public

function

query

($sql)

return $res;

}//獲得最後一條記錄id

public

function

getinsertid()

/** * 查詢某個字段

* @param

* @return string or int

*/public

function

getone

($sql)

//獲取一行記錄,return array 一維陣列

public

function

getrow

($sql,$type=

"assoc"

) $funcname=

"mysqli_fetch_"

.$type;

return

$funcname

($query);}

//獲取一條記錄,前置條件通過資源獲取一條記錄

public

function

getformsource

($query,$type=

"assoc"

) $funcname=

"mysqli_fetch_"

.$type;

return

$funcname

($query);}

//獲取多條資料,二維陣列

public

function

getall

($sql)

return $list;

}public

function

selectall

($table,$where,$fields=

'*',$order=

'',$skip=

0,$limit=

1000

)else}}

elseif(

!empty

($order)

) $sql =

"select $fields from $table where $condition $order limit $skip,$limit"

; $query = $this

->

query

($sql)

; $list =

array()

;while

($r= $this

->

getformsource

($query)

)return $list;

}/**

* 定義新增資料的方法

* @param string $table 表名

* @param string orarray $data [資料]

* @return int 最新新增的id

*/public

function

insert

($table,$data)

//$key的值是每乙個欄位s乙個欄位所對應的值

$key_str.

=$key.

',';

$v_str.

="'$v',";}

$key_str=

trim

($key_str,

',')

; $v_str=

trim

($v_str,

',')

;//判斷資料是否為空

$sql=

"insert into $table ($key_str) values ($v_str)"

; $this

->

query

($sql)

;//返回上一次增加操做產生id值

return $this

->

getinsertid()

;}/* * 刪除一條資料方法

* @param1 $table, $where=array('id'=>'1') 表名 條件

* @return 受影響的行數

*/public

function

deleteone

($table, $where)

}else

$sql =

"delete from $table where $condition"

; $this

->

query

($sql)

;//返回受影響的行數

return

mysqli_affected_rows

($this

->link);}

/* * 刪除多條資料方法

* @param1 $table, $where 表名 條件

* @return 受影響的行數

*/public

function

deleteall

($table, $where)

else}}

else

$sql =

"delete from $table where $condition"

; $this

->

query

($sql)

;//返回受影響的行數

return

mysqli_affected_rows

($this

->link);}

/** * [修改操作description]

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

* @param [type] $data [資料]

* @param [type] $where [條件]

* @return [type]

*/public

function

update

($table,$data,$where,$limit=0)

$str=

rtrim

($str,

',');if

(is_array

($where)

)else}}

elseif(

!empty

($limit)

)else

//修改sql語句

$sql=

"update $table set $str where $condition $limit"

; $this

->

query

($sql)

;//返回受影響的行數

return

mysqli_affected_rows

($this

->link);}

}?>

對db類中__construct()中的配置資訊,進行符合自己資料庫的修改

include 引入db類

使用db類中的方法需要先進行例項化,以插入資料為例:

$db =db:

:getintance()

;$insert_data =

['order_id'

=>

'10010'

,'order_amount'

='200.00'

,'status'

=>1]

;$res = $db-

>

insert

('order_info'

,$insert_data)

;

PHP db連線 資料庫

更詳細點這裡 php 資料庫連線類 class db 連線資料庫 private function db connect 設定字符集 private function db charset 選擇資料庫 private function db usedb 私有的轉殖 private function ...

php db類庫進行資料庫操作

複製 如下 php require once db.php 包含類庫檔案 conn db connect mysql root 1981427 localhost test 連線資料庫 if db iserror conn else 複製 如下 require once db.php conn db...

c 資料庫連線類

c 資料庫連線類 using system using system.data using system.data.sqlclient using system.data.oledb namespace db public class database 根據sql查詢返回dataset物件,如果沒有...