Jump to content

Recommended Posts

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!
Link to comment
https://forums.phpfreaks.com/topic/4691-solved-phpmysql-display-help/
Share on other sites

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 pid
while($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
[!--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 pid
while($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!
A different way is to use a temporary array and the implode() function:
[code]<?php
$tmp = array(); // make sure we start with an empty array
while($row = mysql_fetch_array($sql))
{
    $tmp[] = $row['pid'];
}
$pid = implode(',',$tmp);
?>[/code]

Ken
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.