mckenzie
mckenzie

Reputation: 121

php array into mysql

$sql_where = ''; $exclude = '30,35,36,122,123,124,125';

if($exclude != '') 
{     
    $exclude_forums = explode(',', $exclude); 
    foreach ($exclude_forums as $id) 
    { 
        if ($id > 0) 
        { 
            $sql_where = ' AND forum_id <> ' . trim($id); 
        } 
    }
} 

$sql = 'SELECT topic_title, forum_id, topic_id, topic_type, topic_last_poster_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_time
   FROM ' . TOPICS_TABLE . '
   WHERE topic_status <> 2
      AND topic_approved = 1
      ' . $sql_where . '
   ORDER BY topic_time DESC';

The above code i use to exclude the id of forum to be displayed on sql queries.

Why doesn't it work and still display it?

Any solution

Upvotes: 1

Views: 85

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157989

if($exclude != '') $sql_where = 'AND forum_id NOT IN ($exclude); 

instead of all this messy code :)

Upvotes: 0

Silvio Donnini
Silvio Donnini

Reputation: 3303

You're missing the dot before =

$sql_where .= ' AND forum_id <> ' . trim($id);

Upvotes: 4

zombat
zombat

Reputation: 94237

You are overwriting your $sql_where variable on each loop iteration. You probably want to be using the concatenation operator (.) in the middle of your code instead:

if ($id > 0) 
{ 
    $sql_where .= ' AND forum_id <> ' . trim($id);
}

Note the change of the = to .=

Upvotes: 1

Related Questions