这样可行,它以以下形式返回我的数组:
{"results":[{"cat_id":"8",
"cat_name":"dental hygienist"},
{"cat_id":"5","cat_name":"stocktaker"},
{"cat_id":"9","cat_name":"builder"}]}
我的PHP代码是:
$query2 = "SELECT DISTINCT cat_id,cat_name FROM review WHERE public_or_private = 2";
$result2 = mysqli_query($con,$query2);
$rows = array();
while($row = mysqli_fetch_assoc($result2)) {
$rows['results'][] = $row;
}
echo json_encode($rows);
但是我如何才能与参数化查询相似?
mysqli_query()
,mysqli_store_result()
或mysqli_use_result()
...我尝试了几件这样的事情,但我不断获得null
。
我的PHP代码:
$query3 = "SELECT DISTINCT cat_id,cat_name FROM review WHERE user_id = ? AND public_or_private = 0";
$stmt3 = $con->prepare($query3) or die(mysqli_error($con));
$stmt3->bind_param('i', $user_id) or die ("MySQLi-stmt binding failed ".$stmt3->error);
$stmt3->execute() or die ("MySQLi-stmt execute failed ".$stmt3->error);
$result3 = $stmt3->get_result();
$rows = array();
while ($row = mysqli_fetch_assoc($result3)) {
$rows['results'][] = $row;
}
echo json_encode($rows);
答案 0 :(得分:0)
我不知道为什么,但这对我有用:
$numbervar = 0;
$query3 = "SELECT DISTINCT cat_id,cat_name FROM review WHERE user_id = ? AND public_or_private = ?";
$stmt3 = $con->prepare($query3) or die(mysqli_error($con));
$stmt3->bind_param('ii', $user_id,$numbervar) or die ("MySQLi-stmt binding failed ".$stmt3->error);
$stmt3->execute() or die ("MySQLi-stmt execute failed ".$stmt3->error);
$result3 = $stmt3->get_result();
$rows = array();
while ($row = mysqli_fetch_assoc($result3)) {
$rows['results'][] = $row;
}
echo json_encode($rows);