Solved

export mysql fields

Posted on 2007-04-07
6
211 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

690 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