PHP中批量更新数据表中多条记录
来源:互联网 发布:淘宝网店开店要求 编辑:程序博客网 时间:2024/05/17 07:50
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中批量更新数据表中多条记录
- C#批量更新数据表
- MySQL--数据表更新记录
- php中批量删除Mysql中相同前缀的数据表
- 批量更新具有数千属性列的数据表的记录信息(SQL Server 2005)
- 批量更新记录
- SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int
- PHP批量更新数据库
- php 批量插入记录
- 查询数据表中重复记录
- MySQL数据表中记录不存在则插入,存在则更新/不操作
- PHP对数据表记录的操作
- PHP更新数据库记录
- php动态更新记录
- 使用游标批量更改/填充数据表中的记录值(TheUsingofCursor)
- qeephp 如何批量更新和删除记录?
- mysql语句:批量更新多条记录
- laravel批量更新多条记录
- linux后台运行和关闭、查看后台任务
- Ubuntu12.04下源码安装qt4
- Python 抓取google链接代码
- 实现有弹簧的ScrollView方法:自定义ScrollView
- android 自定义控件库(jar)时属性不能识别的来看吧
- PHP中批量更新数据表中多条记录
- mplayer命令
- hdoj1087 Super Jumping! Jumping! Jumping! ( dp )
- hdu 4302 Holedox Eating
- mongodb语法
- ASP.net 关于TextBox的TextMode=“PassWord”时的动态赋值
- java中的基本方法类型
- 扫雷小游戏
- CWnd与HWND的区别与转换