使用PDO數據庫抽象層往數據表中更新、插入或者刪除數據,都是通過PDO的exec()方法,如果你熟悉Mysql數據庫的sql語句,那麼理解起來就更輕鬆了,你可以把它當作mysql的query()來使用,而且exec()執行完畢還會返回影響的數據行總數。
例如存在表mo2g_test
id | name |
1 | PDO |
2 | mPHP |
我們要更新mo2g_test中id爲1的數據,把他的name值更改爲mysql,那麼mysql的使用方法如下
1 2 3 |
|
PDO的exec()更簡單
1 2 |
|
其實更新數據的sql都有固定的格式:
1)更新一個屬性
1 |
|
2)更新多個屬性
1 |
|
所以,我根據這一特徵封裝了PDO類的update方法
1 |
|
1)$table爲數據表的名稱
2)$arrData爲要更新的數據
3)$condition爲更新條件
這裏我們使用封裝好的update演示一下如何更新上述的數據:
1 2 3 4 |
|
update函數的實現代碼如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
<?php
2 $data = array(array('id' => 1, 'val' => 2), array('id' => 2, 'val' => 3));
3 $update_sql = 'UPDATE tbl_test SET val = CASE id';
4 $params = array();
5 $params[":ids"] = $ids;
6 $in_arr = array();
7
8 foreach($data as $key => $item) {
9 $update_sql .= "WHEN :id_" . $key . "THEN :val_" . $key . " ";
10 $params[":id_" . $key] = $item['id'];
11 $params[":val_" . $key] = $item['val'];
12 $params[":ids_" . $key] = $item['id'];
13 array_push($in_arr, ":id_" . $key);
14 }
15 $update_sql .= "END WHERE id IN (" . implode(',' $in_arr) . ")";
16 TEST::execute($update_sql, $params);//此處會調用bindParam綁定參數
?>
$pdo->select('*','mo2g_test','id=1');//獲取id爲1的數據
$arrData = $pdo->fetch();//獲取找到的數據
print_r($arrData);//輸出找到的數據
$pdo->select('*','mo2g_test','id>1','id');//根據id從大到小進行排序
$pdo->select('*','mo2g_test','id>1','id asc');//根據id從小到大進行排序
$pdo->select('*','mo2g_test','id>1',array('field'=>'id','order'=>'asc'));//根據id從小到大進行排序
$pdo->select('*','mo2g_test','id>1','id','limit 1');//根據id從大到小進行排序,並只獲取1條數據
$arrData = $pdo->fetch_all();//獲取找到的所有數據
print_r($arrData);//輸出找到的數據
//推薦使用的查詢方式
public function query($strSql) {
$this->result = $this->db->query($strSql);
return $this;
}
/*
$select:要獲取的屬性
$table:數據表名稱
$condition:獲取條件
$order:數據排序
$limit:獲取幾條數據
*/
public function select($select,$table,$condition = '',$order = array(), $limit = '') {
if( is_array($order) && isset($order['order']) && ( $order['order'] == 'desc' || $order['order'] == 'asc' ) && isset($order['field']) ) {
$order = "order by $order[field] $order[order]";
} elseif( is_string($order) ) {
$order = "order by {$order}";
} else $order = '';
$this->sql = $strSql = $condition == '' ?
"select $select
from $table
$order
$limit"
:
"select $select
from $table
where $condition
$order
$limit";
if($this->result = $this->db->query($strSql)) {
return $this;
}
return $this;
}
//獲取所有查詢到的數據
public function fetch_all() {
$arrData = array();
if( $this->result ) {
while($row = $this->result->fetch(PDO::FETCH_ASSOC) ) {
$arrData[] = $row;
}
}
return $arrData;
}
//獲取一條查詢到的數據
public function fetch() {
$arrData = array();
if( $this->result ) {
$arrData = $this->result->fetch(PDO::FETCH_ASSOC);
}
return $arrData;
}
public function delete($table,$condition) {
$strSql = "delete from $table where $condition";
if($this->result = $this->db->exec($strSql)){
return $this;
}
return false;
}
使用PHP的PDO擴展進行批量更新操作
<?php
$data = array(array('id' => 1, 'val' => 2), array('id' => 2, 'val' => 3));
$update_sql = 'UPDATE tbl_test SET val = CASE id';
$params = array();
$params[":ids"] = $ids;
$in_arr = array();
foreach($data as $key => $item) {
$update_sql .= "WHEN :id_" . $key . "THEN :val_" . $key . " ";
$params[":id_" . $key] = $item['id'];
$params[":val_" . $key] = $item['val'];
$params[":ids_" . $key] = $item['id'];
array_push($in_arr, ":id_" . $key);
}
$update_sql .= "END WHERE id IN (" . implode(',' $in_arr) . ")";
TEST::execute($update_sql, $params);//此處會調用bindParam綁定參數
?>