Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV output from MySQL -> PHP4

Status
Not open for further replies.

KarveR

MIS
Dec 14, 1999
2,065
GB
Hi all,
I need to get all the names in column2 into an array such as array("item1","item2","item3" etc) in this format.

It seems MySQL normally returns this data like array(item1 *tab* item2 *tab* item3 *tab* etc).

I most definately need the CSV format .. how do I convince MySQL to let me have it?


TIA
Karv
--------------------------------------- I can't have sent that email, it says from Superuser.
 
let me ask you one thing?

You want to do the following SQL?

SELECT name from table

and get all the results in CSV?

if so the code is something like:

$result=mysql_query("SELECT nome from table");
$array=array();

while(list($array[])=mysql_fetch_row($result));
$csv=implode(",",$array);

If is not this what you seek, explain better.

Anikin
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
MySQL can quite easily output CSV right from the command line. See You just need to use the right export options, for example:

SELECT * INTO OUTFILE "/tmp/result.csv"
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "\n"
FROM your_table;


This would export your data from "your_table" with all fields separated by commas, and with text fields additionally surrounded by quotes.

Also the phpMyAdmin system allows for all of this and more from a web page interface, using PHP. Go to I highly recommend it. -------------------

Current reading --
 
Didnt explain it at all well, sorry peeps.. must try a LOT less beer the night before.....
But I did get to what I wanted like this:
----------------------------------------------------------
$result_url = mysql_query ("SELECT url FROM menu",$db);

while($myrow2 = mysql_fetch_array($result_url)) {

$urllink = ($myrow2["url"]);

$Links[] = $urllink;

}
----------------------------------------------------------
which builds me a menu from all items in the column , everytime i want a new link I just add the details and bingo, up she pops ;) I can't have sent that email, it says from Superuser.
 
Ok ... i don't use much MySQL ... only Oracle and SQL Server. :) Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I see...

You just wanted to build an array out of the result column.

One note. You don't need to create an extra variable in between:
Code:
$urllink = ($myrow2["url"]); 
$Links[] = $urllink;

Just do this:
Code:
$Links[] = $myrow2["url"];
-------------------

Current reading --
 
Thanks rycamor, I always appreciate less typing .. and being a n00bi3 to this stuff no doubt I'll write 3 times as much code as I really need to for the time being :)

-------------------------- I can't have sent that email, it says from Superuser.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top