Hello,

I'm having a problem with a script I'm working on, basically, it outputs a SQL query in JSON, however, it's not doing it properly.. E.g.

Right way:

[{"id":111,"title":"Event1","start":"2011-10-10","url":"http:\/\/yahoo.com\/"},{"id":222,"title":"Event2","start":"2011-10-20","end":"2011-10-22","url":"http:\/\/yahoo.com\/"}]

Wrong way:

[{"id":"1","title":"dfssafsaf","start":"2011-10-22","url":"http:\/\/yahoo.com\/"},{"id":"3","title":"dfssafsaf","start":"1-1-1","url":"http:\/\/yahoo.com\/"}[b],][/b]

As you can see it inserts a ",]" and I only want it to show "]" because it's the end of the results..

Here is the code:

<?php

    $dbh=mysql_connect ("localhost", username", "password") or die('Cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("tbl_name");

    $get_events = "SELECT * FROM calendar";
    $get_results = mysql_query($get_events);
    $i;

    if(mysql_affected_rows() >= 1)
    {   
        echo '[';
        while ($post = mysql_fetch_array($get_results))
        {
            $year = $post['year'];
            $month = $post['month'];
            $day = $post['day'];
            $json = array ('id' => $post['calendar_id'],'title' => "dfssafsaf", 'start' => "$year-$month-$day", 'url' => "http://yahoo.com/");
            $output = json_encode($json) . ',';
            echo $output;   
        }   
        echo ']';

      }else{
      echo 'No';

    }
?>

Any suggestions?

Thanks :)

if(mysql_affected_rows() >= 1)
{
  $json = array();	
  while ($post = mysql_fetch_array($get_results))
  {
    $year = $post['year'];
    $month = $post['month'];
    $day = $post['day'];
    $json[] = array ('id' => $post['calendar_id'],'title' => "dfssafsaf", 'start' => "$year-$month-$day", 'url' => "http://yahoo.com/");
  }
  echo json_encode($json);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.