我最初创建了下面的查询以从我的数据库返回一些结果。通过准备好的语句从数据库提取行
$result = mysqli_query($con, "SELECT bookingId, locationName
FROM bookings
WHERE username = '$one'");
$output = array();
while($row = mysqli_fetch_assoc($result)) {
$output[]=$row;
}
print(json_encode($output));
但是,我现在想要使用准备好的语句并尝试了下面的内容。但它总是返回[]
。这是通过准备好的语句返回行的正确方法。
$stmt = $con->prepare('SELECT bookingId,locationName
FROM bookings
WHERE username= ?');
$stmt->bind_param('s', $one);
$stmt->execute();
$stmt->bind_result($id, $loc);
$output = array();
while($row = $stmt->fetch()){
$output[] = $row;
}
$stmt->close();
print(json_encode($output));
要么下降'bind_result'行了,你不要同时使用'$ id'和'$ loc'反正,或在循环中使用它('$输出[$ ID] = $ loc' )。 – raina77ow