The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> PHP Mysqli select to csv
nuddernuby
post 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.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
nuddernuby
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic


Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 05:50 AM