我认为我想要的很简单,但出于某种原因,我被困住了。我有以下内容:
$sql = "...";
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param("sss", $x,$y,$z);
if ($stmt->execute()) {
$result = array(); //not sure if needed
$stmt->bind_result($x1,$y1,$z1); //not sure if needed
//loop resultset and put it in an array ($result);
echo json_encode($result); // convert to json
$stmt->close();
}
}
我看到了fetchAll,fetch_assoc以及更多内容,但我不断将这些调用/函数的错误视为未定义。其他例子是未准备好的陈述。无论我尝试过什么,我都无法使用结果集创建一个数组,我缺少什么?
由于
答案 0 :(得分:0)
使用bind_result
后,您仍然需要获取它们:
$sql = "SELECT col1, col2, col3 FROM table_name WHERE col4 = ? AND col5 = ? AND col6 = ?";
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param('sss', $x,$y,$z);
if ($stmt->execute()) {
$result = array();
$stmt->bind_result($x1,$y1,$z1);
// You forgot this part
while($stmt->fetch()) {
$result[] = array('col1' => $x1, 'col2' => $y1, 'col3' => $z1);
}
echo json_encode($result); // convert to json
$stmt->close();
}
}
或者如果系统上有mysqlnd
驱动程序:
$sql = "SELECT col1, col2, col3 FROM table_name WHERE col4 = ? AND col5 = ? AND col6 = ?";
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param('sss', $x,$y,$z);
if ($stmt->execute()) {
$data = $stmt->get_result();
$result = array();
while($row = $data->fetch_assoc()) {
$result[] = $row;
}
echo json_encode($result); // convert to json
$stmt->close();
}
}