gpt4 book ai didi

php - 在php页面上循环访问数据库中的json数据

转载 作者:行者123 更新时间:2023-11-29 12:01:57 25 4
gpt4 key购买 nike

我在数据库表中有此列:

value=[
{"srno":1,
"name":"Gaspari Aminolast ",
"quantity":"2",
"price":"2920.0000",
"total_bill":5840
},
{"srno":2,
"name":"Gaspari Amino Max ",
"quantity":"2",
"price":"2640.0000",
"total_bill":5280
},
{"srno":3,
"name":"Myofusion 10Lbs",
"quantity":"2",
"price":"8400.0000",
"total_bill":16800}
]

我的 php 代码是:

<?php
$getbill="select value from $tbl where bill_id='$_GET[id]' ";
$result=mysql_query($getbill)or die(mysql_error());
$results = array();
while($row=mysql_fetch_array($result))
{
$results[] = json_decode($row['value']);
}
print_r($results);
foreach($results as $key=>$value){
?>
<tbody>
<tr>
<td><?php echo $value['srno'];?></td>
<td><?php echo $value['name'];?></td>
<td><?php echo $value['quantity'];?></td>
<td><?php echo $value['price'];?></td>
<td ><?php echo $value['total_bill'];?></td>
</tr>
</tbody>
<?PHP
}
?>

我对如何循环遍历它并打印它包含的所有内容感到困惑。

print_r():

Array ( 
[0] => Array (
[0] => stdClass Object (
[srno] => 1
[name] => Gaspari Aminolast
[quantity] => 2
[price] => 2920.0000
[total_bill] => 5840
)
[1] => stdClass Object (
[srno] => 2
[name] => Gaspari Amino Max
[quantity] => 2
[price] => 2640.0000
[total_bill] => 5280
)
[2] => stdClass Object (
[srno] => 3
[name] => Myofusion 10Lbs
[quantity] => 2
[price] => 8400.0000
[total_bill] => 16800
)
)
)

最佳答案

使用 json_decode 中的第二个参数函数,将其设置为 TRUE 以获取数组

$json='[{"srno":1,"name":"Gaspari Aminolast ","quantity":"2","price":"2920.0000","total_bill":5840},{"srno":2,"name":"Gaspari Amino Max ","quantity":"2","price":"2640.0000","total_bill":5280},{"srno":3,"name":"Myofusion 10Lbs","quantity":"2","price":"8400.0000","total_bill":16800}]';


echo '<pre>';print_r(json_decode($json,TRUE));

输出:

Array
(
[0] => Array
(
[srno] => 1
[name] => Gaspari Aminolast
[quantity] => 2
[price] => 2920.0000
[total_bill] => 5840
)

[1] => Array
(
[srno] => 2
[name] => Gaspari Amino Max
[quantity] => 2
[price] => 2640.0000
[total_bill] => 5280
)

[2] => Array
(
[srno] => 3
[name] => Myofusion 10Lbs
[quantity] => 2
[price] => 8400.0000
[total_bill] => 16800
)

)

关于php - 在php页面上循环访问数据库中的json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32227290/

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