?
Solved

export mysql fields

Posted on 2007-04-07
6
Medium Priority
?
215 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
ID: 18871695
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
ID: 18871701
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
ID: 18871721
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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 18871842
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 51

Expert Comment

by:Steve Bink
ID: 18872499
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
ID: 18874540
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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 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