while 循环中的 PHP json_encode()

发布于 2024-11-27 23:22:39 字数 802 浏览 0 评论 0原文

我试图在获取数据库结果时在 while 循环中使用 json_encode() 。这是我的代码:

<?

$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);

$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");

while($row = sqlite_fetch_array($results)) {
  $data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));

sqlite_close($database);

?>

输出是

{“response”:“lastUserID lastUser lastXPos lastYPos”}

我希望它是...

{"response":["1 Alex 10 12", "2 Fred 27 59", "3 Tom 47 19"]}

等。

所以我希望 json_encode() 函数将所有用户放入数组而不是最后一个。我该怎么做?谢谢

I am trying to use json_encode() in a while loop while getting database results. Here is my code:

<?

$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);

$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");

while($row = sqlite_fetch_array($results)) {
  $data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));

sqlite_close($database);

?>

The output of this is

{"response":"lastUserID lastUser lastXPos lastYPos"}

I want it to be...

{"response":["1 Alex 10 12", "2 Fred 27 59", "3 Tom 47 19"]}

etc.

So I want the json_encode() function to put ALL users into the array rather than the last one. How would I do this? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皓月长歌 2024-12-04 23:22:39

尝试:

<?

$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);

$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");

$data = array();

while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));

sqlite_close($database);

?>

Try:

<?

$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);

$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");

$data = array();

while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));

sqlite_close($database);

?>
同展鸳鸯锦 2024-12-04 23:22:39

将此更改

while($row = sqlite_fetch_array($results)) {
  $data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}

$data = array();
while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}

Change this

while($row = sqlite_fetch_array($results)) {
  $data = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}

to

$data = array();
while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
旧梦荧光笔 2024-12-04 23:22:39

将每个用户推送到一个数组:

$data = array();
while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));

Push each user to an array:

$data = array();
while($row = sqlite_fetch_array($results)) {
  $data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文