gpt4 book ai didi

php - 使用 PHP for android 应用程序显示从数据库读取的数据

转载 作者:行者123 更新时间:2023-11-30 01:29:35 26 4
gpt4 key购买 nike

好的,这是我在 php 文件中的代码。我基本上想把这一切都读完

    //Read the username and password from ServerRequest class
$username = $_POST["username"];

//Select all rows when the username match the input
$statement = mysqli_prepare($con, "SELECT * FROM Chore WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);

//store the results of the previous query and create a variable for each column returned
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$chore_name ,$point_value, $parent_username, $child_username, $created, $points_given);
$chore = array();

//store the previous variables you created in the array (only the ones you want to use)
while(mysqli_stmt_fetch($statement)) {
$chore['chore_name'] = $chore_name;
$chore['child_username'] = $child_username;
$chore['point_value'] = $point_value;
}
//encode the array in JSON format
echo json_encode($chore);

//close the statement
mysqli_stmt_close($statement);

//Close the database connection
mysqli_close($con);

我想知道如何读取每个条目以在 android studio 中显示为 ListView ?

最佳答案

您正在重复编写循环中出现的 $chore 数组。

相反,使用获取的内容创建一个临时数组,并将其添加到 $chore 数组的新出现处。

//store the previous variables you created in the array (only the ones you want to use)
while(mysqli_stmt_fetch($statement)) {
$t = array('chore_name' => $chore_name,
'child_username' => $child_username,
'point_value' => $point_value
);
$chore[] = $t;
}
//encode the array in JSON format
echo json_encode($chore);

现在你应该在andriod设备上的java代码中看到一个数组数组

就我个人而言,我会制作一个这样的对象数组

while(mysqli_stmt_fetch($statement)) {
$t = new stdClass();
$t->chore_name = $chore_name;
$t->child_username = $child_username;
$t->point_value = $point_value;

$chore[] = $t;
}

然后 java 将看到一个对象数组。

关于php - 使用 PHP for android 应用程序显示从数据库读取的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959338/

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