如何在文本文件中保存数据库表?以及如何在文本文件中指定格式?

时间:2014-03-28 05:52:58

标签: php mysql

我正在使用脚本在 .txt 文件中保存数据库表。该脚本工作正常。表字段如下:

8,c.s.e,computer ,9,0
9,m.c.a,b.a. in hindi ,10,0

但我希望它像这样 - :

{ "table_name": [
["8","c.s.e","computer","9","0"],
["9","m.c.a","computer","10","0"],
  ]
}

这是我的剧本:

    $fh = fopen('db.txt', 'w');
    $con = mysql_connect("localhost","root","");
    mysql_select_db("dot", $con);
    $result = mysql_query("SELECT * FROM class_master");   
    while ($row = mysql_fetch_array($result)) {          
    $num = mysql_num_fields($result) ;    
    $last = $num - 1;
    for($i = 0; $i < $num; $i++) {            
        fwrite($fh, $row[$i]);                       
        if ($i != $last) {
            fwrite($fh, ",");
        }
    }                                                                 
    fwrite($fh, "\n");
}
fclose($fh);

1 个答案:

答案 0 :(得分:0)

在代码中实现此行

fwrite($fh, json_encode($row[$i]));