日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php怎么更新多条数据,PHP中批量更新数据表中多条记录

發布時間:2023/12/10 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php怎么更新多条数据,PHP中批量更新数据表中多条记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

class test {

/**

* 創建像這樣的查詢: "IN('a','b')";

*

* @author wengxianhu

* @created to 2013-05-27

* @param mix $item_list 列表數組或字符串,如果為字符串時,字符串只接受數字串

* @param string $field_name 字段名稱

* @return void

*/

public function db_create_in($item_list, $field_name = '')

{

if (empty($item_list))

{

return " ".$field_name . " IN ('') ";

}

else

{

if (!is_array($item_list))

{

$item_list = explode(',', $item_list);

foreach ($item_list as $k=>$v)

{

$item_list[$k] = intval($v);

}

}

$item_list = array_unique($item_list);

$item_list_tmp = '';

foreach ($item_list AS $item)

{

if ($item !== '')

{

$item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";

}

}

if (empty($item_list_tmp))

{

return " ".$field_name . " IN ('') ";

}

else

{

return " ".$field_name . ' IN (' . $item_list_tmp . ') ';

}

}

}

/**

* 拼接更新字段

*

* @author wengxianhu

* @created to 2013-05-27

* @param unknown $data 批量更新的數組

* @param string $index_key 主鍵值的字段名

* @return string

*/

public function _concatFields($data, $index_key)

{

if (empty($data)) {

return '';

}

$array_tmp = array();

$index_key_array = array();

if (!is_array(current($data))) {

$data = array($data);

}

$tmp1 = "";

foreach (current($data) as $_v => $_k) {

$tmp1 = $_v . '_temp';

${$tmp1} = "";

if ($_v != $index_key) {

${$tmp1} .= " {$_v} = CASE {$index_key} ";

}

}

reset($data);

$tmp2 = "";

foreach ($data as $_k => $_v) {

foreach ($_v as $_f => $_fv) {

$tmp2 = $_f . '_temp';

${$tmp2} .= "WHEN '{$_v[$index_key]}' THEN '" . addslashes(stripslashes($_fv)) . "' ";

array_push($index_key_array, $_v[$index_key]);

}

}

reset($data);

$tmp3 = "";

foreach (current($data) as $_v => $_k) {

$tmp3 = $_v . '_temp';

${$tmp3} .= " END ";

if ($_v != $index_key) {

$array_tmp[$tmp3] = ${$tmp3};

}

}

$array_tmp[$index_key] = $this->db_create_in($index_key_array, $index_key);

return $array_tmp;

}

/**

* 拼接更新字段

*

* @author wengxianhu

* @created to 2013-05-27

* @param unknown $data 批量更新的數組

* @param string $index_key 主鍵值的字段名

* @return string

*/

public function _concatFields_old($data, $index_key)

{

if(empty($data))

{

return '';

}

$array_tmp = array();

$index_key_array = array();

if(!is_array(current($data)))

{

$data = array($data);

}

foreach (current($data) as $_v => $_k)

{

if($_v != $index_key)

{

${$_v.'_temp'} .= " {$_v} = CASE {$index_key} ";

}

}

reset($data);

foreach ($data as $_k => $_v)

{

foreach ($_v as $_f => $_fv)

{

${$_f.'_temp'} .= "WHEN '{$_v[$index_key]}' THEN '".addslashes(stripslashes($_fv))."' ";

array_push($index_key_array, $_v[$index_key]);

}

}

reset($data);

foreach (current($data) as $_v => $_k)

{

if($_v != $index_key)

{

${$_v.'_temp'} .= " END ";

$array_tmp[$_v.'_temp'] = ${$_v.'_temp'};

}

}

$array_tmp[$index_key] = $this->db_create_in($index_key_array, $index_key);

return $array_tmp;

}

/**

* 獲取更新的數據SQL

*

* @author wengxianhu

* @created to 2013-05-27

* @param unknown $data 批量更新的數組

* @param string $index_key 主鍵值的字段名

* @return multitype:

*/

public function _getUpdateInfo($data, $index_key)

{

reset($data);

$fields = array();

$conditions = array();

$fields_info = $this->_concatFields($data, $index_key);

$conditions = $fields_info[$index_key];

unset($fields_info[$index_key]);

$fields = implode(',', $fields_info);

return compact('fields', 'conditions');

}

/**

* 批量更新數據

*

* @author wengxianhu

* @created to 2013-05-27

* @param unknown $set 批量更新的數組

* @param string $table 數據表名

* @param string $index_key 主鍵值的字段名

* @return void

*/

public function updateAll($set, $table, $index_key)

{

if(empty($set))

{

return '';

}

$update_info = $this->_getUpdateInfo($set, $index_key);

$sql = "UPDATE {$table} SET {$update_info['fields']} WHERE {$update_info['conditions']}";

$this->query($sql,'execute'); //這里根據當前使用的系統修改

}

}

$goods = array(

array('goods_id'=>2000,'cate_id'=>100,'name'=>'godos1'),

array('goods_id'=>2001,'cate_id'=>101,'name'=>'godos2'),

array('goods_id'=>2002,'cate_id'=>102,'name'=>'godos3'),

);

// $goods = array('goods_id'=>2000,'cate_id'=>100,'name'=>'godos1');

$a = new test();

$a->updateAll($goods, 'ecm_goods', 'goods_id');

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的php怎么更新多条数据,PHP中批量更新数据表中多条记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。