Reputation: 5973
I have a domain on Bluehost and need a PHP script which will execute a SQL select and export the data to a CSV file which is saved in some folder on the server. Here is what I tried in PHP:
$result = mysql_query("SELECT * FROM table WHERE MONTHNAME(date) = '$month' AND YEAR(date) = '$year' AND uid = '$uid' INTO OUTFILE 'test.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'");
This was my first attempt but I keep getting permission errors with the folder I am trying to save the file into....which is in www/folder/subfolder
My second attempt is this:
$output = fopen('http://server.com/folder/subfolder/test.csv', 'w+');
fputcsv($output, array('tripid','startingLoc','endingLoc'));
$rows = mysql_query('SELECT tripid, startingLoc, endingLoc FROM autoTracker_trips');
while ($row = mysql_fetch_assoc($rows)) fputcsv($output,$row);
I get the following errors:
[09-Mar-2014 12:43:38 America/Denver] PHP Warning: fopen(http://server.com/folder/subfolder/test.csv): failed to open stream: HTTP wrapper does not support writeable connections in /home4/geronim9/public_html/folder/subfolder/export_csv.php on line 35
[09-Mar-2014 12:43:38 America/Denver] PHP Warning: fputcsv() expects parameter 1 to be resource, boolean given in /home4/geronim9/public_html/folder/subfolder/export_csv.php on line 37
So my question is, where on the server is a good place to save the CSV file...where am I allowed to save from PHP, and which of the two approaches is recommended
Note: above both examples of code above I have the connection info, I just didn`t want to post that.
Thank you!
Upvotes: 2
Views: 2124
Reputation: 23500
You are supposed to use server path and not http path for the function fopen()
to work correctly so change to
/your/path/folder/subfolder/test.csv
Otherwise you will need to activate allow_url_fopen
in your php.ini
Upvotes: 0