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
How to restrict the display of report variants
Hello All,
I want t know how to restrict the display of report variants.
I mean, whe
Display thumbnails as square while retaining aspect ratio
I am trying to figure out a way to make an image display as a square, for example 80x80 pixels, when
Random date selection
i want to select random date with time(hour+minutes+sec) where date is specified(10/22/2009) and tim
how to make database item unique
Hey guys,
is it possible to do this:
I have the database item $title being pulled for
JSON SORT WITH PHP
I have two products that I want to sort by say "Id:17, value: xxx" using php
The page w
redirect standard error and assert (how to?)
How do you redirect standard error and assert?
---
(Why? I am creating an web app and
Undefined variable when using $_SERVER['PHP_SELF']
Hi guyz, please suggest me something...
On first.php I have one input field NAME, and on posting
getting rid of quotes in strings
sick of trying to deal with them in multiple ways (entering in and taking from database, echoing, ec
have trouble in a if condition
The if below is working ok, it check when indexes, name, zipcode and state are empty.
Code: &
is_dir() problem
Hello,
I'm buidling a php scripts that dynamically get's subfolders from a specific folder.