I'm developing an Android application that makes some request to a server in which I have programmed a database. In the server I work with PHP to make query. Now I wrote this code to make a query and to push the result into an array that I can use later in "Android part" by using java language. This is my PHP code:
$query="SELECT * FROM $tbl_name WHERE user='$myusername'";
$res=mysql_query($query);
$posts = array();
if(mysql_num_rows($res)) {
while($post[] = mysql_fetch_assoc($res)) {
array_push($posts, $post);
}
}
echo json_encode(array('posts'=>$posts));
The problem is that when I try to get the result in java I can see only the first row of the result. I'm sure that I have more than 1 row because I tried to print mysql_num_rows($res)
and the result was bigger than 1.
How can I fix this problem??
Edit. Just to clarify. I make a call to my server in which I use php from Android AsynTask. In onPostExecute method I make something like this:
String res = result.toString();
where result is the JSONObject that I obtain from doInBackground method. Maybe I'm doing error Here because all your solution give me always the first row of the query's result only.
答案 0 :(得分:0)
You don't need to specify array here.
Change: while($post[] = mysql_fetch_assoc($res)) {
To: while($post = mysql_fetch_assoc($res)) {
You can always var_dump($posts)
after while
instruction and check if you get what you want.