Solved

export mysql fields

Posted on 2007-04-07
6
206 Views
Last Modified: 2013-12-12
I have an sql query that I would like to export as an csv file.

The sql query is as follows.

SELECT companyname from table1

how do I do this. Note: the table has more than just the company field.

0
Comment
Question by:Cheryl Lander
6 Comments
 
LVL 14

Expert Comment

by:trigger-happy
Comment Utility
Well, the ideal thing to do would be to create a loop that iterates over all the elements in the sql array. For each element, you append a comma to it and put it into a var.

So assuming you have $result as the data returned from mysql_query();

$temp = ""
while($row = mysql_fetch_row($result)){
     $temp = $temp . $row[0] . ", ";
}

The dot is used to append strings.

--trigger-happy
0
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
if you are only exporting one field why do you mean by csv ?
you won't have commans in the files since there is onliy one field
why don't you just redirect the output to a file ?
0
 
LVL 14

Expert Comment

by:trigger-happy
Comment Utility
I believe he wants to create a csv file of all the entries in the database table under the companyname field. Well, that's what i assume anyway.

--trigger-happy
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
here some short code to create a tab-delimited output, you only need to add at the end few lines of code to write the variable $output to a file.
if you want to save also the column names as first line, you just have to add a if ($row_number ==1) in the foreach, and feed a $column_names variable (which you have then to write later in the file first)



$sql =" SELECT * from table1";
$output = "";

if ($rows = mysql_query($sql))
{
  $row_number = 0;
  while (?row = mysql_fetch_assoc($rows))
  {
    $row_number++;
    foreach ( $row as ?field_name => $field_value)  
    {
       $output .= $field_value . "\t";
    }
    $output .= "\n";
  }
}

0
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
Those will work for PHP.  To do it through MySQL, try this:


SELECT companyname INTO OUTFILE '/path/to/csvfile'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM table1;

http://dev.mysql.com/doc/refman/5.0/en/select.html
0
 
LVL 14

Expert Comment

by:ygoutham
Comment Utility
select into outfile syntax returns a error if the target file pre-exists.  so be careful to remove the file once you have exported data to it.  best option would be to safely move it to a temporary folder.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 24
php documentation 4 19
How to add a Chat functions to my PHP site ? 3 17
MySQL left join performance 4 3
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now