我目前正在制作一个网页,旨在显示数据库中的所有内容。所以我做了一个 SQL 命令,它只选择网页上 1 个特定字段所需的数据。
是否可以创建 SQL 命令,以便它一次获取页面的所有内容并且仍然能够单独显示它? 如果是这样,如何?谢谢
function dbGet() {
global $conn;
global $return;
$sql = SELECT * FROM testTable;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$return = $row["text"];
return $return;
}
}
else {
echo "0 results";
}
// $conn->close();
}
最佳答案
你可以通过这种方式来识别记录是否存在。
function dbGet() {
global $conn;
// I am not sure what is the datatype of $return here.
// if it's having a predefined format,
// please push the records into $return instead of creating as an array,
// which will be taken care by framework if any you are using.
// global $return;
$return = array('status' => false, records => []);
$sql = "SELECT text FROM testTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$return['status'] = true;
while($row = $result->fetch_assoc()) {
$return['records'][] = $row["text"];
}
}
// $conn->close();
return json_encode($return);
}
关于php - 如何单独显示SQL结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891683/