Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

export mysql fields

Posted on 2007-04-07
6
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

650 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