April 5, 2012

Concat SQL Array

Question by oussemos

I want to concat two SQL array in order to have a single JSON result

$sql1=mysql_query("select count(Ip_Routeur) as RouteurDown from Routeurs WHERE Etat_Routeur=0");
$sql2=mysql_query("select count(Ip_Routeur) as RouteurUp from Routeurs WHERE Etat_Routeur=1");
while($row1=mysql_fetch_assoc($sql1))
$output1[]=$row1;
print(json_encode($output1));
while($row2=mysql_fetch_assoc($sql2))
$output2[]=$row2;
print(json_encode($output2));
mysql_close();

Answer by Starx

The codes does not required to combine any array.

You can combine, the two query results with a single query

$sql1=mysql_query("select count(Ip_Routeur) as RouteurDown from Routeurs WHERE Etat_Routeur=0 or Etat_Routeur=1");
$rows = array();
while($row1=mysql_fetch_assoc($sql1)) { 
    $rows[] = $row1;
}
print(json_encode($rows));
mysql_close();

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!