1

I have this code in PHP:

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=calls.csv');

// create a file pointer connected to the output stream
$output = fopen($_SERVER["DOCUMENT_ROOT"].'/file_dump/price_tariffs/calls.csv', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2'));

// loop over the rows, outputting them
$sql="SELECT * from call_costs where sequence < '50' ";
$rs=mysql_query($sql,$conn);
while($result = mysql_fetch_array($rs)) {
    fputcsv($output, $result["number"]);
}

its creating the file name calls.csv in the price_tariffs directory but its only adding the column 1 and column 2 and not the data from the while loop

i have check the loop and echoed data inside the loop which displays fine

2
  • 1
    What is the print_r output for one of $result["number"]? Commented Jan 10, 2015 at 22:01
  • also, do you want to save it to a file, or make the browser download the content? Commented Jan 10, 2015 at 22:10

4 Answers 4

3

fputcsv takes the second parameter as an array(), "and you already used fputcsv outside of the loop passing the second param as an array"[*] with two values inside. Try to do the same inside your loop:

fputcsv($output, array($result["number"], $result["somethingelse"]));

[*]: edited, added enquoted sentence after clarifying in the comments below.

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

3 Comments

I think it is because of the incorrect information about fputcsv where you have said that it takes the second parameter with two values. Indeed, it takes the second parameter with just an array with any length of values, not restricted to two values only.
Oh, I definitely got it wrong because of my poor english: of course I didn't mean that the second param array holds only two values. I was somehow (in my mind) referring to the fact that he had already stored two values on the first fputccsv [fputcsv($output, array('Column 1', 'Column 2'));] and so I assumed he wanted to output again two columns... anyway, it was my fault, downvote earned :P
I just fixed my answer adding a note referring to these comments.
0

Select only the columns that you want:

$sql = "SELECT column1, column2 FROM call_costs WHERE sequence < '50'";

Then use mysql_fetch_assoc() to fetch each row as an associative array, and output that:

$rs=mysql_query($sql,$conn);
while($row = mysql_fetch_assoc($rs)) {
    fputcsv($output, $row);
}

fputcsv()'s second argument is supposed to be an array of the values that should be put into fields in the CSV file.

Comments

0

since you're sending the data to the client directly, you should echo it instead of saving it to a file :) Open the php://output instead:

Try this (from powtacs answer:

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=calls.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2'));

// loop over the rows, outputting them
$sql="SELECT * from call_costs where sequence < '50' ";
$rs=mysql_query($sql,$conn);
while($result = mysql_fetch_assoc($rs)) {
    fputcsv($output, $result);
}

Also please note that mysql is depreciated, and you should use mysqli or PDO instead

Comments

0

Checkout the mysql_fetch_array in the manual, so, we can say:

...
while($result = mysql_fetch_array($rs,MYSQL_NUM)) {
    fputcsv($output, $result);
}

Just add the second optional parameter MYSQL_NUM to return numerical array and supply all of it as a parameter in fputcsv. By this way you will get all the raw's fields data in your file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.