gpt4 book ai didi

php - 如何使用 PHP 对 SQL 语句中的多行进行 JSON 编码

转载 作者:行者123 更新时间:2023-11-28 21:54:19 24 4
gpt4 key购买 nike

我正在构建一个使用网络服务获取数据的 iOS 应用程序。 Web服务由PHP、MySQL数据库组成。

我已经成功地对返回的数据进行了 JSON 编码,但我使用的代码似乎只对 1 行进行了编码。

我想获得一些关于如何对多行进行编码的建议?

谢谢。

<?php
$username = "root";
$password = "*******";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

//print "Connected to MySQL<br>";

$selected = mysql_select_db("newtest",$dbh)
or die("Could not select first_test");

$query = "SELECT * FROM MyGuests ";
$result=mysql_query($query);

echo json_encode(mysql_fetch_assoc($result));

?>

最佳答案

$query = "SELECT * FROM MyGuests ";
$result=mysql_query($query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print_r json_encode($rows);

关于php - 如何使用 PHP 对 SQL 语句中的多行进行 JSON 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27130925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com