php - 如何使用 php 从 sql 数据库中获取所有用户
问题描述
In$result
应该是来自数据库的所有用户,但它只需要第一人称并显示错误。
我的PHP代码:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once 'connect.php';
$response = mysqli_query($conn, "SELECT imie, nazwisko FROM users");
$result = array();
$result['osoby'] = array();
$row = mysqli_fetch_assoc($response);
$index['name'] = $row['imie'];
$index['surname'] = $row['nazwisko'];
array_push($result['osoby'], $index);
$result['success'] = "1";
echo json_encode($result);
}
解决方案
您必须循环结果数组。
$resultJson = array();
$resultJson['osoby']=array()
$query = "SELECT imie,nazwisko FROM users";
$result = $mysql->query( $query );
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
// fetch information out of the $row..
$resultJson['osoby'][] = ['name' => $row['imie'], 'surname' => $row['nazwisko']];
}
}
print json_encode($resultJson);
推荐阅读
- sql - 需要帮助在oracle中对数据进行排序
- python - 三线性插值 1820:错误:-215:断言失败
- c# - Azure Function - 如何启动 excel 文档
- r - 如何使用 gsub 在 for 循环中找到完全匹配?
- javascript - 如果站点从短信中的链接打开,则在触摸之前不会触发 beforeinstallprompt
- python - 如何从我抓取的网页中删除空格?
- react-native - 如何模拟从 npm 模块导入的类
- delphi - Delphi 10.3.2 - 将项目添加到版本控制 - 导入到 SVN 崩溃 Delphi IDE
- r - R Windows:安装包“rvest”(和其他几个包)时出错
- javascript - 如何处理nodejs应用程序中的“ReferenceError:require is not defined”?