This works, it returns my array in the form:
{"results":[{"cat_id":"8",
"cat_name":"dental hygienist"},
{"cat_id":"5","cat_name":"stocktaker"},
{"cat_id":"9","cat_name":"builder"}]}
My php code is :
$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);
But how can I get similar to work with parameterised queries?
mysqli_query()
, mysqli_store_result()
or mysqli_use_result()
... I've tried several things like this but I keep getting null
.
My php code:
$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);