Array to string conversion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Array to string conversion

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array to string conversion

Post 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!";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Array to string conversion

Post 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 .=
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: Array to string conversion

Post by IGGt »

Cheers for that guys, I see that I was nearly there, but just had things in the wrong order.
Post Reply