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.

GautamD31
  • 28,552
  • 10
  • 64
  • 85
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
  • 3,149
  • 2
  • 29
  • 44
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);
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0
while($row = mysql_fetch_assoc($result))
{
    $new_arr[] = array("total"=>$row['SUM(total)']);
}
echo json_encode($new_arr);
Harry Bomrah
  • 1,658
  • 1
  • 11
  • 14