This is my php code:
$query = Select * from tablename;
$result = mysql_query($query) or die((mysql_error()));
$count = 0;
if (mysql_num_rows($result) > 0){
while( $row = mysql_fetch_array($result)){
$json_output[$count]=$row;
$count ++;
}
}
$output = json_encode($json_output);
$output = str_replace('[{', '{', $output);
$output = str_replace('}]', '}', $output);
echo $output;
And this is a part of my php output:
{"0":{"0":"2","Gi_Id":"2","1":"sample_name.jpg","Gi_nome_file":"sample_name.jpg","2":"sample_name","Gi_Pseudonimo":"s. name","3":"sample name","
as you can see, there are the tag "0", "1" , "2", etc that represent the column number ( "0" for Gi_Id , "1" for Gi_nome_file etc) of my sql table. Why there are this duplicates? Probally i can manage this output ignoring the duplicate single cell value, but i want to undestand how to fix that.
I think that the problem is this row of my code:
$json_output[$count]=$row;
but i don't find another way to get all my table data.
Any suggestions?
EDIT
Using mysql_fetch_assoc i dont have the duplicate but remains the number at the begining of each row, my output is the following:
{"0":{"Gi_Id":"2","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name",
and for the row number 2 i have:
{"1":{"Gi_Id":"3","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name",
and for the row number 3 i have:
{"2":{"Gi_Id":"4","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name",
...
and for the row number ni have:
{"n":{"Gi_Id":"n+1","Gi_nome_file":"sample_name.jpg","Gi_Pseudonimo":"s. name",
etc i would like to remove the {"0": , {"1": ,{"2":, {"3": at the begining of each row , how i can do this?
I solve my problem:
this is the solution:
$result = mysql_query($query) or die((mysql_error()));
$json_output = array(); $count = 0;
$response["Prova"] = array(); if (mysql_num_rows($result) > 0){
while( $row = mysql_fetch_assoc($result)){
$json_output[$count]=$row;
// push single rating into final response array
$count ++;
}
array_push($response["Prova"], $json_output);
}
//alla fine aggiungo il tag di successo
$json_output["success"] = 1;
$output = json_encode(array($response));
//rimuovo le parentesi graffe per far si che non mi dia problemi con il json di android
$output = str_replace('[{', '{', $output);
$output = str_replace('}]', '}', $output);
echo $output;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.