0

I'm currently have a recordset created with dreamweaver and have encode the results in json format which work fine.

Recordset

$maxRows_rs_feeds = 3;
$pageNum_rs_feeds = 0;
if (isset($_GET['pageNum_rs_feeds'])) {
  $pageNum_rs_feeds = $_GET['pageNum_rs_feeds'];
}
$startRow_rs_feeds = $pageNum_rs_feeds * $maxRows_rs_feeds;

mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_feeds = "SELECT fname,lname FROM profile";
$query_limit_rs_feeds = sprintf("%s LIMIT %d, %d", $query_rs_feeds, $startRow_rs_feeds, $maxRows_rs_feeds);
$rs_feeds = mysql_query($query_limit_rs_feeds, $vivalooks) or die(mysql_error());
$row_rs_feeds= mysql_fetch_assoc($rs_feeds);

if (isset($_GET['totalRows_rs_feeds'])) {
  $totalRows_rs_feeds = $_GET['totalRows_rs_feeds'];
} else {
  $all_rs_feeds = mysql_query($query_rs_feeds);
  $totalRows_rs_feeds = mysql_num_rows($all_rs_feeds);

}
$totalPages_rs_feeds = ceil($totalRows_rs_feeds/$maxRows_rs_feeds)-1;

 do { 

 echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));

mysql_free_result($rs_feeds);

Recordset Results

{"fname":"Benjamin","lname":"Blay"}{"fname":"Alfread","lname":"Mark"}{"fname":"yaa","lname":"tiwaa"}

But i want the results to be encode like this rather

[{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}]
neiza
  • 265
  • 4
  • 15
  • 1
    You should learn about [why shouldn't you use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Ekin Jun 09 '16 at 08:24

3 Answers3

0

Try:

do { 
     $rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));

echo json_encode($rows);
Pavel Petrov
  • 847
  • 10
  • 19
  • thanks but its not working, this is how the results shows up: ["{\"fname\":\"Benjamin\",\"lname\":\"Blay\"}","{\"fname\":\"Alfred\",\"lname\":\"Mark\"}","{\"fname\":\"yaa\",\"lname\":\"tiwaa\"}" I want it in this format rather [{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}] – neiza Jun 09 '16 at 08:36
  • paste your new implementation here, please – Pavel Petrov Jun 09 '16 at 08:44
  • Implementation as in? I don't get you – neiza Jun 09 '16 at 08:58
  • Sorry, I found the error. Try the code now. First tiem I forgot to remove the json_encode within the loop. its not needed there. – Pavel Petrov Jun 09 '16 at 09:26
0

Try

do { 
    $json[]=json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "[".implode(", ", $json)."]";

OR

do { 
     $rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));

echo json_encode($rows);
SML
  • 1,235
  • 6
  • 17
  • its putting [ at the start of every fname and ] at the end of every lname. i want all the three variables to be enclosed in it – neiza Jun 09 '16 at 09:00
0

You can change your loop to look like this:

echo "[";
$count = 0;
do { 
    if($count !== 0) 
        echo ",";
    $count++;
    echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "]";

$count is there just to skip first "," so you don't get [,{...},{...}] or you can put it like false/true... I just can't remember right now what is better in case you have 10k steps loop...

Bozidar Sikanjic
  • 717
  • 5
  • 12