birdie Posted March 11, 2006 Share Posted March 11, 2006 hi, i'm sure this is easy but i have completely forgot how to do this kind of thing so i'm after help :-).i have a column that is named 'pid'.[code]while($row = mysql_fetch_array($sql){echo $row['pid']; //this gives all of the pid's..}[/code]how could i echo the pid's like this..1,2,3,4,5 insead of 12345?thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/4691-solved-phpmysql-display-help/ Share on other sites More sharing options...
wildteen88 Posted March 11, 2006 Share Posted March 11, 2006 Simple do this:[code]while($row = mysql_fetch_array($sql)){ echo $row['pid'] . ',';}[/code]and you'll get a result of 1,2,3,4,5,If you want to not show the ending , after 5 then do this:[code]$pid = ''; //setup the variable pidwhile($row = mysql_fetch_array($sql)){ $pid .= $row['pid'] . ',';}//this removes the last character from value in $pid$pid = substr($pid, 0, strlen($pid)-1);[/code]You should now get a result like 1,2,3,4,5 Quote Link to comment https://forums.phpfreaks.com/topic/4691-solved-phpmysql-display-help/#findComment-16446 Share on other sites More sharing options...
birdie Posted March 11, 2006 Author Share Posted March 11, 2006 [!--quoteo(post=353951:date=Mar 11 2006, 04:44 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 11 2006, 04:44 PM) [snapback]353951[/snapback][/div][div class=\'quotemain\'][!--quotec--]Simple do this:[code]while($row = mysql_fetch_array($sql)){ echo $row['pid'] . ',';}[/code]and you'll get a result of 1,2,3,4,5,If you want to not show the ending , after 5 then do this:[code]$pid = ''; //setup the variable pidwhile($row = mysql_fetch_array($sql)){ $pid .= $row['pid'] . ',';}//this removes the last character from value in $pid$pid = substr($pid, 0, strlen($pid)-1);[/code]You should no get a result like 1,2,3,4,5[/quote]ok thanks! Quote Link to comment https://forums.phpfreaks.com/topic/4691-solved-phpmysql-display-help/#findComment-16447 Share on other sites More sharing options...
kenrbnsn Posted March 11, 2006 Share Posted March 11, 2006 A different way is to use a temporary array and the implode() function:[code]<?php$tmp = array(); // make sure we start with an empty arraywhile($row = mysql_fetch_array($sql)){ $tmp[] = $row['pid'];}$pid = implode(',',$tmp);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/4691-solved-phpmysql-display-help/#findComment-16477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.