[英]Android, Volley,SQL Only one Record being displayed?
so after much trouble shooting i have finally gotten my list view working. 因此,在排除了很多麻烦之后,我终于使我的列表视图起作用。
Essentially my table has 3 records which should match id 1. I want these to be displayed in my android listview, only 1 is displayed. 本质上,我的表有3条记录,它们应该匹配ID1。我希望这些记录显示在我的Android列表视图中,仅显示1条。 I assume my php is correct as when i enter myurl://retreiveData.php?id=1
我认为我的php是正确的,就像我输入myurl://retreiveData.php?id = 1
All 3 records are displayed, how ever the android application only displays 1 显示所有3条记录,而android应用程序仅显示1
I will post my php below, and the extract from the java which loops through the json data 我将在下面发布我的php,以及从json数据循环访问的java的摘录
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('dbConnect.php');
$sql = "SELECT * FROM Exercises WHERE id='".$id."'";
$r = mysqli_query($con,$sql);
while($res = mysqli_fetch_array($r)){
$result = array();
array_push($result,array(
"muscle group"=>$res['muscle group'],
"exercise"=>$res['exercise'],
)
);
echo json_encode(array("result"=>$result));
}
mysqli_close($con);
}
Move the line $result = array();
移动行
$result = array();
above your while
loop in PHP. 在PHP的
while
循环之上。
On every iteration you are creating a new object. 在每次迭代中,您都在创建一个新对象。 On browser you are able to see because you print the value in each iteration but at the end of the
while
loop, it will only have the last value. 在浏览器上,您可以看到,因为您在每次迭代中都打印了该值,但是在
while
循环结束while
,它将仅具有最后一个值。 You can test it by echoing the value of $result
after the while
loop. 您可以通过在
while
循环后回显$result
的值来进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.