techtheatre Posted March 19, 2007 Share Posted March 19, 2007 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! Link to comment https://forums.phpfreaks.com/topic/43320-solved-removing-double-sql-data-with-implode/ Share on other sites More sharing options...
btherl Posted March 19, 2007 Share Posted March 19, 2007 Try var_dump($row), and you will see the problem After that, try adding the MYSQL_ASSOC option, as mentioned here: http://sg2.php.net/manual/en/function.mysql-fetch-array.php Link to comment https://forums.phpfreaks.com/topic/43320-solved-removing-double-sql-data-with-implode/#findComment-210358 Share on other sites More sharing options...
techtheatre Posted March 19, 2007 Author Share Posted March 19, 2007 excellent! thanks for the var_dump() function...that will be very useful in the future! I added the MYSQL_ASSOC parameter to my code and it works perfectly! THANKS!!! Link to comment https://forums.phpfreaks.com/topic/43320-solved-removing-double-sql-data-with-implode/#findComment-210365 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.