NameSilo

Selecting rows from database with different ids

Spaceship Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
Hi all i am trying to select topics from many forums (in phpbb3) and this is the code:

PHP:
	if (preg_match ("/^([0-9.,-]+)$/", $forum)) 
	{
		$sql = $db->sql_build_query('SELECT',
		array(
		'SELECT'   => 'p.*, u.*',
		'FROM'      => array(
			POSTS_TABLE   => 'p',
			USERS_TABLE   => 'u',
		),
		'WHERE'      => "p.topic_id = '{$topics[$i]['topic_id']}'
			AND p.forum_id = '{$forum}'
			AND p.poster_id = u.user_id",
		'ORDER_BY'   => "post_id ASC"
		));
	}

And you see the "($forum)" well it currently only works if i use one number, but what if i want to select from more than one forum, how do i use more numbers in there?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
are you not able to place that in a for loop, like:

for(i=0;i>=4;i++){

$forum = i;
//code here;

}

like so
 
0
•••
I really don't understand that :(
 
0
•••
PHP:
 if (preg_match ("/^([0-9.,-]+)$/", $forum)) 
    {
        $sql = $db->sql_build_query('SELECT',
        array(
        'SELECT'   => 'p.*, u.*',
        'FROM'      => array(
            POSTS_TABLE   => 'p',
            USERS_TABLE   => 'u',
        ),
        'WHERE'      => "p.topic_id = '{$topics[$i]['topic_id']}'
            AND p.forum_id IN('{$forumids}')
            AND p.poster_id = u.user_id",
        'ORDER_BY'   => "post_id ASC"
        ));
    }
Where $forumids would be a comma separated list of forum id's
 
0
•••
Thanks a lot someone on devshed said the same, but note you can't put '' around the (1,3,5) or it gives an error.

So it would read:

PHP:
if (preg_match ("/^([0-9.,-]+)$/", $forum)) 
    {
        $sql = $db->sql_build_query('SELECT',
        array(
        'SELECT'   => 'p.*, u.*',
        'FROM'      => array(
            POSTS_TABLE   => 'p',
            USERS_TABLE   => 'u',
        ),
        'WHERE'      => "p.topic_id = '{$topics[$i]['topic_id']}'
            AND p.forum_id IN ({$forum})
            AND p.poster_id = u.user_id",
        'ORDER_BY'   => "post_id ASC"
        ));
    }

Thanks buddy :)

Ps. it needed to stay as "$forum" as well, as it is used throughout my script :)
 
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back