【发布时间】:2017-09-02 01:02:08
【问题描述】:
我有一个“Android”项目,我需要从数据库中获取一些信息。数据库是“MySQL”。我必须通过网络服务,因为我之前从未做过任何 PHP,我几乎看不到问题,因为逻辑对我来说似乎没问题。
这是我的代码:
<?php
require 'connect.php';
$username = 'alex@hotma';
$password = 'soleil';
$sql = 'SELECT ID, NAME, PASSWORD, EMAIL FROM Account';
#$sql = 'CALL Login('.$username .', '. $password .')';
$result = $conn->query($sql);
$response = array();
if (!empty($result))
{
if($result->num_rows > 0)
{
$response['success'] = 1;
$users = array();
while($row = $result->fetch_assoc())
{
$user = array();
$user["id"] = $row["ID"];
$user["name"] = $row["NAME"];
$user["password"] = $row["PASSWORD"];
$user["email"] = $row["EMAIL"];
//Trying to add the user into my users array
//If i uncomment this line everything is shown, but not in the response array
//echo json_encode($user);
array_push($users, $user);
}
$response['users'] = $users;
}
else
{
$response['success'] = 0;
$response['message'] = 'No user found';
}
}
else
{
$response['success'] = 0;
$response['message'] = 'No user found';
}
echo json_encode($response);
$conn->close();
?>
我的数据库中目前有超过 6 个用户,但我似乎无法全部获取。我已经对我的一些代码进行了注释,因此您可以看到我尝试过的内容,可惜没有任何成功。我想返回一个包含我所有用户的“JSON”数组。
你们中有人知道如何进行吗?
【问题讨论】:
-
$response['user']或$response['users']? -
我更喜欢 $response['users'];我看到我在那里留下了一些不受欢迎的代码,我的错
-
那么你的脚本的输出是什么?
-
目前没有任何显示。但是,如果我取消注释循环中的“回声”,一切都会出现,只是不会出现在“$response”中。我希望响应包含数组 users 中的所有用户。
-
顺便说一句,# 不是 PHP 中的注释,请使用 // 或 /* */