Solved

export mysql fields

Posted on 2007-04-07
6
207 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 142

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 50

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

22 Experts available now in Live!

Get 1:1 Help Now