gpt4 book ai didi

php - 将查询结果作为行存储到数据库表中

转载 作者:行者123 更新时间:2023-11-29 14:26:00 24 4
gpt4 key购买 nike

查询结果

Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)

[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
..........

我想将此结果存储到另一个表中作为行库存。该表将如下所示:

ID        inventory
1 (the result)
2 (another result)

之后我会再次查询它,以便我可以显示结果。

这是我最近所做的事情。

商店:

//getting the result
$inv = $this->db->get_from('table','id'=>'1')->row();
<input type="hidden" name="inventory" value="<?php print_r($inv)?>">
//storing in the new table
$this->db->insert('table2',array('inventory'=>$this->input->post('inventory')));

获取:

$inventory = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
//result
array
(
[ID] => 1
[inventory] =>
array
(
[0] => stdClass Object
(
[ingredientID] => 2
...... and so on

我想显示数组['inventory']中的所有内容,它是一个对象数组。

我已经做到了 foreach($arr['inventory'] as $invent): echo $invent['ingredientID'];

但是foreach部分有错误。错误:为 foreach() 提供的参数无效

我该怎么办? 结束foreach;

最佳答案

假设:

$results = $this->db->get_where('table2',array('ID'=>'1'))->row_array();

你应该用它来打印它

foreach($results['inventory'] as $inventory)
{
print_r($inventory->ingredientID);
}

关于php - 将查询结果作为行存储到数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10946003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com