1

I have a search query as

$sql="SELECT user_id,user_name,user_occupation FROM profile WHERE user_email LIKE '%peace%'";

$result=mysql_query($sql);
$jsonResult = '{"results" : [ ';
while ($data=mysql_fetch_assoc($result))
{
$jsonResult .=json_encode($data).',';
}
$jsonResult .= ']}';

echo $jsonResult;

After referring How to remove last comma (,) from array? , i added

$sql="SELECT user_id,user_name,user_occupation FROM profile WHERE user_email LIKE '%peace%'";

$result=mysql_query($sql);
$jsonResult = '{"results" : [ ';
while ($data=mysql_fetch_assoc($result))
{
$jsonResult .=json_encode($data).',';
}
$jsonResult .= ']}';

$finalResult = rtrim(',',$jsonResult);
echo $finalResult;

This gives me an output as ...

{"results" : [ {"user_id":"2","user_name":"peace","user_occupation":"Web Developer"},  
               {"user_id":"3","user_name":"lover","user_occupation":"Web Developer"}
               {"user_id":"4","user_name":"User4","user_occupation":"Developer"},
               {"user_id":"5","user_name":"User5","user_occupation":"Developer"},]}

I want to remove the last comma from the results array

Community
  • 1
  • 1
Peace Lover
  • 93
  • 2
  • 12
  • 1
    When you iterate over your results, instead of appending to a JSON string, add the results to an array. Then when you are done iterating, convert that array to json via `json_encode`. You avoid any JSON syntax issues this way. – cheeken Nov 18 '11 at 06:53

6 Answers6

5

Instead of building a string within the loop, populate an array instead, with each element being a single associative array (row) from your result set. Then, after the loop, simply json_encode the array you've built.

Christopher
  • 764
  • 4
  • 6
2

Do like this

$result=mysql_query($sql);
$jsonResult = '{"results" : [ ';
$i=0;
while ($data=mysql_fetch_assoc($result)) {
   if($i != 0){
       $jsonResult .=',';
   }
   $jsonResult .=json_encode($data);
   i++;
}
$jsonResult .= ']}';
Selvakumar Ponnusamy
  • 5,363
  • 7
  • 40
  • 78
1

rtrim() removes characters from the end of the string. As you've added ]}, the comma is no longer at the end.

Trim the comma before adding the last part:

$finalResult = rtrim(',',$jsonResult);
$finalResult .= ']}';

Although, as Christopher says, it's not a very good idea to build a JSON string manually.

JJJ
  • 32,902
  • 20
  • 89
  • 102
0

Push your INNER array into another array like this:

...
$result=mysql_query($sql);
$final_result=json_encode(['result' => $result]);

All done. Have fun!

MojSkin
  • 81
  • 1
  • 5
0

can u try like filling some array from the incoming result and then just json encode that array like

..push result row arrays to $result ..
$data = array('results' => $results);
json_encode($data);
satin
  • 697
  • 2
  • 7
  • 19
0
$sql="SELECT user_id,user_name,user_occupation FROM profile WHERE user_email LIKE '%peace%'";

$result=mysql_query($sql);
$jsonResult = '{"results" : [ ';
$counter = 1;
while ($data=mysql_fetch_assoc($result))
{
if($counter!=1)
 {
 $jsonResult .= ','.json_encode($data);
 }
else
 {
 $jsonResult .=json_encode($data);
 }
$counter++;
}
$jsonResult .= ']}';
$finalResult = rtrim(',',$jsonResult);
echo $finalResult;
Elias Hossain
  • 4,410
  • 1
  • 19
  • 33