gpt4 book ai didi

php - 遍历数组并将值传递给mysql查询php

转载 作者:行者123 更新时间:2023-11-30 21:39:02 25 4
gpt4 key购买 nike

我有以下 JSON 对象

[
{
"var1": "ID001",
"var2": "ROY",
"var3": "16"
},
{
"var1": "ID002",
"var2": "MARK",
"var3": "15"
},
{
"var1": "ID003",
"var2": "PETER",
"var3": "15"
}
]

我想遍历 JSON 并将值传递给以下 MySQL 查询

$select_data=mysqli_query($connsow, "select * from salary where col1 = '$var1' and col2 = '$var2'");

下面是我的代码,我使用了一个 foreach 循环,但它总是选择 JSON 对象中的第一个值。我无法遍历整个 JSON 并将值传递给查询

foreach($rows as $item) { 
$cust_ord_no = $item['var1'];
$cont_n = $item['var2'];
$select_data=mysqli_query($connsow, "select * from test where col1 = '$var1' and col2 = '$var2'");
}

最佳答案

  • 首先使用 json_decode 将 JSON 转换为数组第二个参数设置为 true 的函数。然后,您可以遍历数组。

执行以下操作:

$rows = json_decode($rows, true);

关于php - 遍历数组并将值传递给mysql查询php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52586310/

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