0

Hi I have some table say fro example table1 ,table2 in my mysql databse. I need to get following json result from php. Can anyone suggest how to achieve this? any good tutorial doing same is also helpful. I am able to convert database result in simple json response but custom response is something difficult for me.

{
response:ok
tables:[
{
    name:table name
        data:[
                {
                fieldname1:value1
                fieldname2:values2
                },
                {
                fieldname1:value1
                fieldname2:value2
                }
                .
                .
            ]               
},
{
    name:table name1
        data:[
                {
                fieldname1:value1
                fieldname2:values2
                },
                {
                fieldname1:value1
                fieldname2:value2
                }
                .
                .
            ]               
},

]
}
}
1
  • 1
    You will need to create a custom array with the data you want and the organisation you want, and the call json_encode with this array. Commented Oct 9, 2013 at 13:13

1 Answer 1

1

Quoting from How to convert mysql data base table data in json using php, once you have your table names you can do for each of them.

$result = array();
$result['response'] = 'ok'
foreach ($tables as $tableName) {       
    $query = mysql_query("SELECT * FROM $tableName");
    $rows = array();
    while($row = mysql_fetch_assoc($query)) {
        $rows[] = $row;
    }
    $result['tables'][] = array(
         'name' = $tableName,
         'data' = $rows
    )
}
print json_encode($result);
Sign up to request clarification or add additional context in comments.

2 Comments

@ leorossi Hi,We are not using $result anywhere in our json output ?I guess we should use it.What do you say?
I just mispelled the variable name. $result is what you are jsonizing and echoing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.