refiking Posted August 24, 2007 Share Posted August 24, 2007 I created the csv file, but it only shows the last record in my db. How can I make it show all of the records? Here's the code: <?php include 'connect.php'; $que = mysql_query("SELECT * FROM `CP`"); while($row = mysql_fetch_assoc($que)){ $bfn = $row['bfn']; $bln = $row['bln']; $bssn = $row['bssn']; $bdob = $row['bdob']; $bemp = $row['bemp']; $bjt = $row['bjt']; $cjten = $row['cjten']; $bjmi = $row['bjmi']; $bjten = $row['bjten']; $bb1 = $row['bb1']; $bb1a = $row['bb1a']; $bb = $row['bb2']; $bb2a = $row['bb2a']; $bb3 = $row['bb3']; $bb3a = $row['bb3a']; $cfn = $row['cfn']; $cln = $row['cln']; $cdob = $row['cdob']; $cssn = $row['cssn']; $cemp = $row['cemp']; $cjt = $row['cjt']; $cb1 = $row['cb1']; $cb1a = $row['cb1a']; $cb2 = $row['cb2']; $cb2a = $row['cb2a']; $cb3 = $row['cb3']; $cb3a = $row['cb3a']; $addy = $row['addy']; $zip = $row['zip']; $taxes = $row['taxes']; $ins = $row['ins']; $tenure = $row['tenure']; $value = $row['value']; } $data = ""; $row = $bfn.","; $row .= $bln.","; $row .= $bssn.","; $data .= $row."\n"; header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=log.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo $data; ?> Quote Link to comment https://forums.phpfreaks.com/topic/66544-solved-csv-exporting/ Share on other sites More sharing options...
Barand Posted August 24, 2007 Share Posted August 24, 2007 header stuff while ($row = mysql_fetch_assoc($que)){ get data echo data } Quote Link to comment https://forums.phpfreaks.com/topic/66544-solved-csv-exporting/#findComment-333265 Share on other sites More sharing options...
refiking Posted August 24, 2007 Author Share Posted August 24, 2007 If I do that, how can I separate the data into the specified fields? I don't want every field to be imported into the csv file. Just the ones listed. Quote Link to comment https://forums.phpfreaks.com/topic/66544-solved-csv-exporting/#findComment-333271 Share on other sites More sharing options...
Barand Posted August 24, 2007 Share Posted August 24, 2007 You would do it the same way, it's just that at the moment every time you read a record you are overwriting the previous values, so you are just left with the last record's values. You need to write the record within the loop. But why retrieve the whole row when you only need 3 cols? <?php include 'connect.php'; header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=log.csv"); header("Pragma: no-cache"); header("Expires: 0"); $sql = "SELECT bfn,bln,bssn FROM `CP`"; $que = mysql_query($sql); while ($row = mysql_fetch_row($que)) { $data = join (',', $row) . "\n"; echo $data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66544-solved-csv-exporting/#findComment-333284 Share on other sites More sharing options...
refiking Posted August 24, 2007 Author Share Posted August 24, 2007 The cols that I am retrieving are not all there. There are more than that. I just did the first three to test the csv file before I continued. I'm testing this out now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/66544-solved-csv-exporting/#findComment-333326 Share on other sites More sharing options...
refiking Posted August 24, 2007 Author Share Posted August 24, 2007 Works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/66544-solved-csv-exporting/#findComment-333334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.