sparedog Posted October 9, 2009 Share Posted October 9, 2009 Hi all, I am new to php and new to this board. I have tried searching but cannot find anything for this. If I echo the following code, it places all the results in a line, without spaces or line breaks. How would I separate the data by adding spaces or any other character for that matter. Thanks $sql = mysql_query("SELECT friendwith FROM friends WHERE member = '$myuname'"); while ($row = mysql_fetch_array($sql)) { $result= "$row[friendwith]"; } Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/ Share on other sites More sharing options...
Bricktop Posted October 9, 2009 Share Posted October 9, 2009 Hi sparedog, Amend your code to read: $sql = mysql_query("SELECT friendwith FROM friends WHERE member = '$myuname'"); while ($row = mysql_fetch_array($sql)) { $result .= ''.$row[friendwith].'<br />'; } The above code is adding a line break to the end of each $row[friendwith] which is getting output. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/#findComment-933729 Share on other sites More sharing options...
sparedog Posted October 9, 2009 Author Share Posted October 9, 2009 No sorry that didn't seem to do it. I'll give you my whole code, maybe i'm doing something wrong elsewhere. What I'm trying to do is remove any duplicates from the final result $myfinal and I thought by separating, I could do this. Maybe there is another way to remove the duplicates from the $myfinal? $myfriend_list = mysql_query("SELECT example FROM table1 WHERE member = '$myuname'"); while ($row1 = mysql_fetch_array($myfriend_list)) { $result1= "$row1[example]"; $myoutput = mysql_query("SELECT example2 FROM table2 WHERE username ='$result1'"); while ($row2 = mysql_fetch_array($myoutput)) { $result2= "$row2[example2]"; $myfinal = mysql_query("SELECT * FROM table3 WHERE example3 ='$result2'"); echo $myfinal; }} Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/#findComment-933738 Share on other sites More sharing options...
Bricktop Posted October 9, 2009 Share Posted October 9, 2009 Hi sparedog, From your above code you're echoing $myfinal but that's a MySQL query, all this will return is a Resource Identifier. Have a look here for a tutorial on the DISTINCT statement which will remove duplicates. Hope this helps. Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/#findComment-933744 Share on other sites More sharing options...
kickstart Posted October 9, 2009 Share Posted October 9, 2009 Hi Not quite sure what you are trying to do as you appear to just be echoing out the result from your 3rd query. What you could do is add each result to an array, and then once all are brought back you could use array_unique to get rid of the duplicates before outputting them. However it would appear a better idea to use a single piece of SQL and use distinct (but whether this is useable depends on how complex you real SQL is rather than your slimmed down example). Something like:- SELECT distinct c.* FROM table1 a INNER JOIN table2 b ON a.example = b.username INNER JOIN table3 c ON b.example2 = b.example3 WHERE a.member = '$myuname' All the best Keith Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/#findComment-933746 Share on other sites More sharing options...
sparedog Posted October 9, 2009 Author Share Posted October 9, 2009 yes sorry. i threw that echo on there without thinking about it. my real script actually goes on to say what's below. I was trying to not complicate things by throwing the echo on the end of the script I posted, but in doing so, I did complicate things The script functions, but it prints duplicates. Please ignore the script below it is only to show that the echo was added for the purpose of the post. // Define $color=1 $color="1"; echo '<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">'; while ($row3 = mysql_fetch_array($myfinal)) { // If $color==1 table row color = #FFC600 if($color==1){ echo "<tr bgcolor='#F0F0F0'> <td>".$row3['example4']."</td><td>".$row3['example5']."</td><td>".$row3['example6']."</td> </td> </tr>"; // Set $color==2, for switching to other color $color="2"; } // When $color not equal 1, use this table row color else { echo "<tr bgcolor='#D8D8D8'> <td>".$row3['example4']."</td><td>".$row3['example5']."</td><td>".$row3['example6']."</td> </tr>"; // Set $color back to 1 $color="1"; } } } } echo '</table>'; mysql_close(); ?></body></html> Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/#findComment-933750 Share on other sites More sharing options...
sparedog Posted October 9, 2009 Author Share Posted October 9, 2009 Yup, DISTINCT did the trick. Thanks all Link to comment https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/#findComment-933819 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.