Page 1 of 1

Array to string conversion

Posted: Wed Dec 16, 2009 9:28 am
by IGGt
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I am looking to write a MySQL query to a csv file.

At the minute I:

check for the original file, and delete it;
create a new (blank) file;
run the MySQL query and pass it into an array;
write it to the new file;

But it is missing something, as it only writes the first line from the query.

my code so far is:

Code: Select all

 
<?php
//Delete Original File
    ...
 
//Set variables
    $uName = "root";
    $pWord = "password";
    $host = "localhost";
 
    $Query = "[i]...MySQL Query goes here...[/i];";
 
    $Filename = str_replace('.php', '', basename("Report")) . '_' . date("Ymdhis") . '.csv';
 
    $ReportFiles = "c:\ReportFiles\\";
 
    $Newfile = $ReportFiles.$Filename;
 
    $cr = "\n";
 
//Create a new (empty) file
    echo exec("<nul (set/p z=) > \"$Newfile\"");
 
//Run MySQL Query
    $connection = mysql_connect($host, $uName, $pWord) ;
    mysql_select_db("dbName", $connection) ;
    $result = mysql_query ($Query, $connection);
    while ($row = mysql_fetch_array($result, MYSQL_NUM))
        $String = implode(",",$row);
        {
 
//Open file and save contents of query to that file
        $fp = fopen($Newfile,"a");
            if($fp){
                    fwrite($fp,$String.$cr);
                    fclose($fp);
                echo "File saved successfully";
        }  else {
                echo "Error saving file!";
        }
    }
?>
 
What is missing?


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Array to string conversion

Posted: Wed Dec 16, 2009 10:23 am
by AbraCadaver
I think your { after the while is misplaced. Also, why open and close the file each time through the loop? Just open it, loop through and save data, and then close it. You shouldn't need to delete or create the file either. Opening the file for writing will create it if it doesn't exist or truncate it if it does exist:

Code: Select all

//Don't need this, fopen() will do it
//echo exec("<nul (set/p z=) > \"$Newfile\"");
 
//Open file for writing, create it if it doesn't exist
 $fp = fopen($Newfile,"w");
 if($fp) {
//Run MySQL Query
    $connection = mysql_connect($host, $uName, $pWord) ;
    mysql_select_db("dbName", $connection) ;
    $result = mysql_query ($Query, $connection);
//Loop through and save data
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        $String = implode(",",$row);
        fwrite($fp,$String.$cr);
    }
//Close file
    fclose($fp);
    echo "File saved successfully";
} else {
    echo "Error saving file!";
}

Re: Array to string conversion

Posted: Wed Dec 16, 2009 10:24 am
by pickle
Your whole file delete/create can be replaced with one call to fopen() with the 'w' parameter.

Your problem is you're re-assigning $String every loop. You probably want to concatenate with .=

Re: Array to string conversion

Posted: Wed Dec 16, 2009 11:06 am
by IGGt
Cheers for that guys, I see that I was nearly there, but just had things in the wrong order.