jsd*_*jsd 8 codeigniter sql-update
我一直在四处寻找,但我还没有找到答案.如果你知道答案,请帮忙.
如何更新CI中的多行?
在我的MySQL中:
我有列名:
ID, Settings Name, Settings Value ( Settings Name is Unique )
Run Code Online (Sandbox Code Playgroud)
我有ff数据:
ID = 1, Settings Name = "Hello" , Settings Value = "True"
ID = 2, Settings Name = "World", Settings Value = "Good"
Run Code Online (Sandbox Code Playgroud)
和更多 ...
我也有一个表格,Settings Value但我不知道如何在数据库上更新它.如何更新True为Hello正在Settings Name更新Good为World.
我听说过,insert_batch()但有update_batch()吗?
小智 17
你使用的是Active唱片吗?
是的,有一个更新批次:$ this-> db-> update_batch();
$data = array(
array(
'ID' => 1 ,
'Settings Name' => 'Hello' ,
'Settings Value' => 'True'
),
array(
'ID' => '2' ,
'Settings Name' => 'World' ,
'Settings Value' => 'Good'
)
);
$this->db->update_batch('mytable', $data, 'where_key');
Run Code Online (Sandbox Code Playgroud)
从文档:
第一个参数将包含表名,第二个参数是值的关联数组,第三个参数是where键.
update_batch() CodeIgniter 中确实有一个可用的方法。
您可以像这样使用它的示例:
$data = array(
array(
'ID' => 1,
'Settings Name' => 'Hello',
'Settings Value' => 'True'
),
array(
'ID' => 2,
'Settings Name' => 'World',
'Settings Value' => 'Good'
)
);
$this->db->update_batch('tableName', $data, 'id');
Run Code Online (Sandbox Code Playgroud)
所以你拥有的是一个数组数组,孩子们基本上保存了数据库中每一行的数据。for 第一个参数update_batch()是数据库表的名称,第二个是$data变量,第三个是您要在WHEN子句中使用的列。