PHP Mysqli select to csv |
PHP Mysqli select to csv |
nuddernuby |
Aug 27 2015, 04:16 AM
Post
#1
|
Group: Members Posts: 3 Joined: 27-August 15 Member No.: 23,453 |
Could someone save me from insanity and provide a working script for the following simple problem, please. (I've tried many and failed)
I have a table 'projects' with field 'projectname', currently populated with five records, say A,B,C,D & E. I need to store an the array of these records in this format: $arr=("A","B","C","D","E") or output it to a csv string in csv file. I can echo the results but fail to get this into the required format/file. Basic query is: $query=mysqli_query($conn,"SELECT projectname FROM projects"); What then? Apologies for the simplicity of the problem. |
nuddernuby |
Aug 27 2015, 12:44 PM
Post
#2
|
Group: Members Posts: 3 Joined: 27-August 15 Member No.: 23,453 |
Thank you, I finally managed to get it working using this simple script with $proj as the required output:
QUOTE $query = "SELECT * FROM `projects` WHERE co='ABC' "; $result = mysql_query($query); $number = mysql_numrows($result); $x = 0; $proj = array(); while ($x < $number) { $row = mysql_fetch_array($result); $id = $row['id']; $p = $row[ 'projectname' ]; $proj[ $id ] = $p; $x++; } //print_r($proj); $arr=implode(',',$proj); //print_r('<br>' . $arr); file_put_contents('NEWfilename.txt', print_r($proj, true)); So, all well. Thank you for your attention. |
Lo-Fi Version | Time is now: 27th April 2024 - 11:51 AM |