help with selecting / separating values

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

help with selecting / separating values

Post by zerodegreeburn »

OK, in phpmyadmin this works ok

SELECT `forumid`
FROM `forum`
WHERE 1 AND `countid` = 5 LIMIT 0 , 30

this brings up 3 forums, but everytime i've tried to put them in a string, it seems to bunch them all together, so say they were '1' '5' and '11' the string would be "1511".

any way separating the values into x number of strings (in PHP)?

sorry for the db newbie
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

php returns db results in array format so it is probably working.

Just loop through the results and print em like this:

Code: Select all

foreach($pub as $beer){
echo "Value: ".$beer;
}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could we see the code you're using to put them into a string.

Mac
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

i used this code to get it partly working:

Code: Select all

<?php

$forumid = $DB_site->query('SELECT forumid FROM forum WHERE countid=5 ORDER BY forumid');
while ($forumids = mysql_fetch_array($forumid)):    
	$forum_ids .= "$forumids[forumid],";
endwhile;

?>
this makes it come out as $id1,$id2,$id3,

what i'm trying to do is use this code in another sql query:

WHERE forumid IN ($id1,$id2,$id3)

so if i could just get rid of the last , then it would work :)
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try this:

Code: Select all

<?php 

$result = $DB_site->query('SELECT forumid FROM forum WHERE countid=5 ORDER BY forumid'); 

while ($row = mysql_fetch_array($result)) {
   $forum_ids[] = $row['forumid']; 
}

$forum_ids = implode(',', $forum_ids);

?>
Mac
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

thanks, but i got it to work like this and i dont want to chagne anything now :D

Code: Select all

<?php

$idforum = $DB_site->query('SELECT forumid FROM forum WHERE countid=5 ORDER BY forumid');
while ($forumids = mysql_fetch_array($idforum)):    
	$forum_ids .= "$forumids[forumid],";
endwhile;

// get rid of last comma
$forum_ids = substr("$forum_ids", 0, -1); 

?>
then i can just use it like this:

WHERE forumid IN ($forum_ids)

thanks for the help
Post Reply