0

I have the following code:

<?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("coockie");

    $filename ="excelreport.xls";
    $contents = "date \t ip \t visits \t \n";

    $result=mysql_query("select * from test");

    $mydate='';
    $myip='';
    $visits='';

    while($data=mysql_fetch_assoc($result))
    {
        $mydate.=explode(",", $data['date']);
        $myip.=explode(",", $data['ip']);
        $visits.=$data['visits'];

    }

    print_r($mydate);

    //header('Content-type: application/ms-excel');
    //header('Content-Disposition: attachment; filename='.$filename);
    //echo $contents;
?>

$mydate is outputted as string Array. I need it outputted like array of values. Any suggestions?

1
  • Please clearify - there is a recursion in your question ;) Commented Aug 19, 2011 at 11:08

2 Answers 2

2

You are misusing string concatenation operator .=

$mydate.=explode(",", $data['date']);

explode gives you an array, and with .= it's converted to string Array. The proper way is to use [] operator

$mydate=array();
$myip=array();
$visits='';

while($data=mysql_fetch_assoc($result))
{
    $mydate[]=explode(",", $data['date']);
    $myip[]=explode(",", $data['ip']);
    $visits.=$data['visits'];

}

print_r($mydate);
Sign up to request clarification or add additional context in comments.

Comments

2

You might want to try var_dump instead of print_r. At least for debugging purposes.

var_dump($mydate);

2 Comments

Yes, that's a good suggestion. It works just like print_r but will also show you the types of all data.
Plus that it can prettyformat the output as readable html, and it will recursively dump the members (privates as well) of all types of objects.