Problem in response format

Hi, can i ask some help on this format on how to achieve,I am requesting data to the server via ajax and i want my response to be like this

{“name”:“john”,“fname”:“doe”,“memeberid”:[“1234, 3568”,“3456,7777”]}// this is the response in my firebug



 $result =  mysql_query("SELECT * FROM tbl_mem where mem_id='id'
            ");

    $mem='';
    $mem.="[";
    while($row=mysql_fetch_array($result,MYSQL_BOTH)){

        $mem.=".".$row['memid_one'].",".$row['memid_two'].".";
    }

   $mem.="]";


    $arr=array('name'=>$name,//i have no problem on here
                    'fname'=>$fname,//i have no problem on here
                    'memeberid'=>$mem);//here i have problem


    return json_encode($arr);


Thank you in advance.

Hi jemz, how are you?

First of all (and I’m sure you’ve heard this before around the forums) you shouldn’t be using the mysql_* functions. They’ve been deprecated and will be removed from PHP at some point. It’s a good idea to switch to using either the msqli or PDO extension.

As for your code, rather than trying to build the JSON array as a string, just use a PHP numeric array and let json_encode to do the conversion:


$member_id = array();
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{ 
    $member_id[] = $row['memid_one'] . ", " . $row['memid_two']; 
}

$arr = array('name' => $name, 'fname' => $fname, 'memberid' => $member_id);

Hi FretBurner :slight_smile:

I’am fine,yes I know that mysql is deprecated, I was the one who continue the project where i worked so it’s big work if i recode the project…
Thank you for the quick reply,and i will let you know if this is working for me

Thank you again :slight_smile:

Hi FretBurner,It’s workng i can see now the returned data format as i expected :slight_smile:

Thank you.