0

On converting data from JSON to CSV format I am getting output like below in CSV file. Any possible suggestions ?

0,"","","0","0","0","0","","","0","0","","","","0","0","0","0","0","0","","","","","","0","0","0","0","","0","","","0","0","0","","0","0","0","0","0","0","0","","","","","","","","","","","","","","","","","","","","","","","","","","","0","0","","","","","","","","0","0",","",""

my function

function csv($form){


$fp = fopen($form, "w");

$sql = "SELECT * FROM log AS jon 
                    LEFT JOIN r as rec ON jon.num=rec.num
                    LEFT JOIN b as bio ON jon.num=bio.num   
                    LEFT JOIN p as po ON jon.num=po.num
                    LEFT JOIN re as recur ON jon.num=recur.num
                    LEFT JOIN s as spir ON jon.num=spir.num
                    LEFT JOIN te as ten ON jon.num=ten.num
                    LEFT JOIN tt as ttai ON jon.num=ttai.num
                    LEFT JOIN vi as vis ON jon.num=vis.num
                    LEFT JOIN ac as acui ON jon.num=acui.num
                    LEFT JOIN au as aud ON jon.num=aud.num
                    LEFT JOIN e as ec ON jon.num=ec.num
                    LEFT JOIN ha as han ON jon.num=han.num
                    LEFT JOIN pe as peri ON jon.num=peri.num
                    LEFT JOIN pr as pre ON jon.num=pre.num";
$res=mysql_query($sql);
if(!$res) die ('Unable to run query:'.mysql_error());


    while ($row = mysql_fetch_assoc($res))
{
    //echo implode(', ', $row)."\r\n";

   fputcsv(STDOUT, $row);
}        

In PHP file

$rslt = mysql_query("SELECT * FROM ".$table." WHERE num = ".$id."") or die("Unable to Execute QueryThe error was" . mysql_error());                 
$record = mysql_fetch_assoc($rslt);
var jsonObject = JSON.stringify($record);    
2
  • 4
    Why are you JSON-encoding the data, when you actually want it in CSV format? Commented Jun 19, 2012 at 10:38
  • then what should i do ??? thank u for ur promt reply Commented Jun 19, 2012 at 10:50

3 Answers 3

2

There's also fputcsv() for this:

$rslt = mysql_query("SELECT * FROM ".$table." WHERE no = ".$id."") or die("Unable to Execute QueryThe error was" . mysql_error());
while ($row = mysql_fetch_assoc($rslt))
{
    fputcsv(STDOUT, $row);
}

This function takes care of values containing commas.

Sign up to request clarification or add additional context in comments.

Comments

0
$rslt = mysql_query("SELECT * FROM ".$table." WHERE no = ".$id."") or die("Unable to Execute QueryThe error was" . mysql_error());
while ($row = mysql_fetch_assoc($rslt))
{
    echo implode(', ', $row)."\r\n";
}

Note that the solution above assumes that you don't have , in your fields.

2 Comments

I am still not getting data in desired format. In excel sheet it is not coming in row and column wise . but just like text as shown above .please help.Thank u
Looks like valid CSV data to me. Are you saving it as a .csv file, and opening with a spreadsheet program?
0
mysql_connect("localhost","root","");
mysql_select_db("test");
$rslt = mysql_query("SELECT * FROM alok") or die("Unable to Execute QueryThe error was" . mysql_error());                 
$record = mysql_fetch_assoc($rslt);
$jval = json_encode($record);

echo $jval;
database
name(field) ["6","1","1"]
Result
{"name":"[\"6\",\"1\",\"1\"]"}

but your data previously saved by json decode format.

1 Comment

I am still not getting data in desired format. In excel sheet it is not coming in row and column wise . but just like text as shown above .please help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.