0

I'm using this code as mentioned here.

$file = date('dmY-His');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=visitors-$file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$sql = "select id, ip, server, time, date from visitors";
$res = mysql_query($sql);
$data = array();
$data[] = array('id', 'ip', 'server', 'time', 'date');
while ($row = mysql_fetch_array($res)) {
    $data[] = array_values($row);
}

$output = fopen("php://output", "w");
foreach ($data as $val) {
    fputcsv($output, $val);
}
fclose($output);

First of all, this program in not working on my localhost but works fine on server. Why?
Second, the data I'm getting contains double entries, i.e.,

+----+----+---------+---------+-----------+
| id | ip | server  | time    | date      |
+----+----+---------+---------+-----------+
| 1  | 1  | :::1    | :::1    | server1   | server1  | 10:00:00 am | 12-12-2012 |
+----+----+---------+---------+-----------+----------+-------------+------------+
| 2  | 2  | :::2    | :::2    | server2   | server2  | 10:15:00 am | 13-12-2012 |
+----+----+---------+---------+-----------+----------+-------------+------------+
3
  • Enable error reporting and see if outputs any error messages. doesn't work alone isn't going to be useful if you want accurate answers. Commented Sep 25, 2013 at 13:42
  • What does it act on your localhost? Commented Sep 25, 2013 at 13:48
  • it just gives me a php file Commented Sep 25, 2013 at 15:01

1 Answer 1

1

Change mysql_fetch_array() to mysql_fetch_assoc(). mysql_fetch_array() fetches both a numeric and associative array of the database results.

while ($row = mysql_fetch_array($res)) {

to

while ($row = mysql_fetch_assoc($res)) {
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know what the real issue is but the OP said not working on my localhost but works fine on server

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.