i have a json array generated by this php:
$filtros=mysql_query("SELECT id_transporte FROM user_trans WHERE id_usuario = '".$id_usuario ."'");
for($i=0; $i<=mysql_num_rows($filtros); $i++){
$rs[]= mysql_fetch_array($filtros,MYSQL_ASSOC);
}
foreach($rs as $valor){
if (is_array($valor)) {
foreach($valor as $valor1){
$reportes[]= mysql_query("
SELECT * FROM transportes
WHERE id_transporte='".$valor1."'
ORDER BY fecha_reporte DESC ");
}
}
}
foreach($reportes as $valor){
if($valor != false){
for($i=0; $i<=mysql_num_rows($valor); $i++){
$row[]= mysql_fetch_array($valor,MYSQL_ASSOC);
}
}
}
print_r (json_encode($row));
which returns:
[{"id_report":"73","id_transport":"624","txt_report":"Report0"},
{"id_report":"46","id_transport":"624","txt_report":"Report1"},false,
{"id_report":"74","id_transport":"9999","txt_report":"Report2"},
{"id_report":"52","id_transport":"9999","txt_report":"Report3"},false]
well, i try to read that, but java only read until "false"... that array are 2 arrays which joined and printed, so i try this:
$row1=str_replace ( "false" , '{"salto":1}' , $row );
$row1[]=false;
print_r (json_encode($row1));
that returns the sames but "" instead "false" and a "false" in the end but java continues reading until now the "" i use that for read json
if (jdata!=null && jdata.length() > 0){
JSONObject json_data = null;
try{
System.out.println( jdata.length() );//java there print all the array length ignoring the "false" o ""
for (int i=0; i < jdata.length(); i++){
reportsId[i]=jdata.getJSONObject(i).getString("id_report");
transportId[i]=jdata.getJSONObject(i).getString("id_transport");
txt_report[i]=jdata.getJSONObject(i).getString("txt_report");
System.out.println( " i= "+ i);
}
}
}catch(Exception e3){}
so my question is how read this, or change some lines on php
First of all in your PHP you shouldn't use mysql_*
functions as they are deprecated and dangerous. Your code could be vulnerable to SQL Injection. Consider using PDO
You could probably make this much faster by running a single query that would also get rid of the false values. You certainly don't need to stuff everything into an array and then loop over it.
Here's my take:
$resp = mysql_query("SELECT t.* FROM transportes t
JOIN user_trans ut ON t.id_transporte=ut.id_transporte
WHERE ut.id_usuario='$id_usuario'
ORDER BY fecha_reporte DESC");
if(!$resp) {
// handle the error!
die(mysql_error())
}
$output = [];
while( $row = mysql_fetch_assoc($resp) ) {
$output[] = $row;
}
print_r(json_encode($output));
This should solve your problem with false values because they are likely due to the second query not being checked for errors.
The JSON in not well formed.
There's a comma missing after "id_report":"46"