0

I want this kind of JSON array, database1 all tables

{
    {"table1":"[{"key11":"val11"},{"key12":"val12"},{"key13":"val13"}]"},
    {"table2":"[{"key21":"val21"},{"key22":"val22"},{"key23":"val23"}]"},
    {"table3":"[{"key31":"val31"},{"key32":"val32"},{"key33":"val33"}]"},
    {"table4":"[{"key41":"val41"},{"key42":"val42"},{"key43":"val43"}]"},
    {"table5":"[{"key51":"val51"},{"key52":"val52"},{"key53":"val53"}]"},
}

This bellow code is convert a single database table to JSON array, But i want entire database tables to JSON array

    $return_arr = array();
    $fetch = mysql_query("SELECT * FROM table1");   
    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
        $row_array['val2'] = $row['val1'];
        $row_array['val2'] = $row['val2'];
        $row_array['val3'] = $row['val3'];      
        array_push($return_arr,$row_array);
    }   
    echo json_encode($return_arr);

anyone help me please.

4
  • you mean whole database or particular table Commented Jun 18, 2015 at 11:00
  • yes whole database, i have code to particular table. I want to convert particular database and all tables in above format, answer please. Commented Jun 18, 2015 at 11:04
  • you want Only tables name or whole entries from Tables also. Commented Jun 18, 2015 at 11:36
  • I want whole entries from Tables and all tables from a single database, answer please. Commented Jun 18, 2015 at 11:37

3 Answers 3

0

Iterate over all tables, you can use that query to get all tables.

select * from information_schema.tables

You still need to select all results inside the tables.

0

something like this:

$db='recipes';
$tables=$db->query('SHOW TABLES IN '.$db, O_ARRAY);
foreach($tables as $n=>$v){
    $table=$v['Tables_in_'.$db];
    $array=$db->query("SELECT * FROM $table");
    $data[$table]=$array;
}
echo '<pre>';
echo json_encode($data);

that's going to be pretty memory intensive on large data! You might add some filters for unneeded fields, or database-specific fields that need to be skipped or the value translated to another place.

2
  • yes, i want this way of format, but can i run empty page is displaying, please can you simplify to WAMP server. mysql_connect('localhost','root',''); mysql_select_db('recipes'); $db='recipes'; $tables=mysql_query('SHOW TABLES IN '.$db); foreach($tables as $v) { echo $table=$v['Tables_in_'.$db]; $array=mysql_query("SELECT * FROM $table"); $data[$table]=$array; } echo '<pre>'; echo json_encode($data); this code displaying output is null, please just correct my code. Commented Jun 18, 2015 at 11:30
  • mysql_connect('localhost','root',''); mysql_select_db('recipes'); $db='recipes'; $tables=mysql_query('SHOW TABLES IN '.$db); foreach($tables as $v) { echo $table=$v['Tables_in_'.$db]; $array=mysql_query("SELECT * FROM $table"); $data[$table]=$array; } echo '<pre>'; echo json_encode($data); Commented Jun 18, 2015 at 11:42
0

This is the answer for above question change database name only all tables and table records are given in JSON array format.

    mysql_connect('localhost','root','');
    mysql_select_db('database');
    $db='database'; 

    $return_arr = array();
    $return_arr1 = array();
    $return_arr2 = array(); 
    $return_arr3 = array();
    $result = mysql_query("SHOW TABLES IN ".$db); 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        array_push($return_arr,$row);
    }
    foreach($return_arr as $key => $value)
    {
        $table=$value['Tables_in_'.$db];
        $array1=mysql_query("SELECT * FROM $table");        
        while ($row1 = mysql_fetch_array($array1, MYSQL_ASSOC)) 
        {
            array_push($return_arr3,$row1);
        }       
            $return_arr1[$table]=$return_arr3;
            unset($return_arr3);
            $return_arr3 =array();
    }
    echo '<pre>';
    print_r($return_arr1);
1
  • other than the db connection, this answer is basically the same as what I specified. As you have 4 different arrays going, mine might take a bit less memory. But as mine uses special db methods, it might be a toss-up Commented Jun 18, 2015 at 18:20

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.