我有以下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'");
}
答案 0 :(得分:0)
true
的函数。然后,您可以遍历数组。执行以下操作:
$rows = json_decode($rows, true);
答案 1 :(得分:0)
您尝试使用名称访问键,而不是使用名称,而必须使用在SQL之前定义的变量。
$rows = json_decode($strJSON, true);
$select_data = [];
foreach( $rows as $item ) {
$cust_ord_no = $item['var1'];
$cont_n = $item['var2'];
$strSQL = "select * from test where col1 = '$cust_ord_no' and col2 = '$cont_n'";
$select_data[] =mysqli_query($connsow, $strSQL);
}