Convert array to string and write to csv file
Posted on
16th Feb 2014 07:03 pm by
admin
I am looking to write a MySQL query to a csv file.
At the minute I:
Did you know?Explore Trending and Topic pages for more stories like this.
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: <?php
//Delete Original File
...
//Set variables
$uName = "root";
$pWord = "password";
$host = "localhost";
$Query = "[i]...MySQL query...[/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!";
}
}
?>
No comments posted yet
Your Answer:
Login to answer
239
46
Other forums
Help with ORDER BY
Hello. I would like to order by ascending States, then Cities, then Gyms in the following code, but
Do something every fifth time?
I'm trying to write a loop, but I want it to do something different after every fifth instance. Like
drop-down with sub-category appear
Hello,
i know how to build a simple dro-down list, im looking for a code when im gonna choose
MVC - Code review
I'm in the process of trying to wrap my head around MVC, and as part of that, I'm attempting to impl
the problem with str_replace
$str="hahahahahahahahahahahahahaha";
$nn=1;
$str=str_replace('ha','MyGod',$str,$nn);
Displaying a record from mysql in a simple swf file
Hi,
I have a mysql database containing information I would like to display in my swf.
data type in column definition
Hi,
I create a table with column called "Direction of Travel code ".
The travel codes in t
mysql VARCHAR acting like INT
Hi, All.
I have a table that contains a varchar(10) column named weird_field. In this column
php email checker
I am having alot of trouble finding info on how to do these email things. I need to do the following
=> and <=
So I was digging through some code when I came across the <= operator. This is the first tim