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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.