Jump to content

[SOLVED] Removing double SQL data with IMPLODE()


techtheatre

Recommended Posts

The code below is a simplified version of what i am doing.  I am trying to create a comma separated list of values pulled from a table in MySQL.  This is for an easy export function of some of my stored data.  I thought that I could be clever and save some typing by using the implode() function in PHP rather than using explode() and then listing every variable in a row separated by a comma.  The good news is that it seems to have MOSTLY worked, except that now I am getting two of everything.  :-\  I think it has something to do with the $row array having multiple values for each key (maybe?...i am still not comfortable with arrays).  Anyway, what do i need to do to get a single list of values, separated by a comma (and kept in the order returned by the query)?  THANKS!

 

$data = "";

$sql = "SELECT * FROM some_table WHERE something ORDER BY fieldname ASC";
$result = @mysql_query($sql);

while( $row = mysql_fetch_array($result) ){
    $data .= implode(",",$row);  //take all returned variables and separate them by commas
    $data .= "\n";  // add line-break at the end
}

 

EXAMPLE CURRENT OUTPUT:

LastName,LastName,FirstName,FirstName,Address,Address,City,City\n

 

SHOULD BE:

LastName,FirstName,Address,City\n

 

 

THANKS! ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.