0

I'm having a hard time to parse this PHP to JSON kan someone help me with this how can I convert this PHP into JSON

$query = "SELECT SUM(total)  FROM account";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
echo $row['SUM(total)'];
}

4 Answers4

1

Try with json_encode like

$query = "SELECT SUM(total) as total  FROM account";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
    $new_arr[] = $row['total'];
}
echo json_encode($new_arr);

Try to avoid using mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.

Gautam3164
  • 28,027
  • 10
  • 58
  • 83
1
$query = "SELECT SUM(total) as t  FROM account";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
    $new_arr[] = $row['t'];
}
echo json_encode($new_arr);
Andy Gee
  • 2,915
  • 2
  • 26
  • 39
0

use json_encode() function in php.

Check the reference here

while($row = mysql_fetch_assoc($result))
{
    $myArray[] = $row['SUM(total)'];
}
echo json_encode($myArray);
Deepu
  • 11,587
  • 13
  • 55
  • 88
0
while($row = mysql_fetch_assoc($result))
{
    $new_arr[] = array("total"=>$row['SUM(total)']);
}
echo json_encode($new_arr);
Harry Bomrah
  • 1,648
  • 1
  • 11
  • 14