bklups
bklups

Reputation: 300

SQL query with multiple parameters in where

I have a simple database like this :

Name     id
-----------
music    1
sport    2
theatre  3

I just want to display

music

theatre

I call a function with the ids and want to show name corresponding, my query looks like this, but nothing happen

$array = array(1,3);
$id = implode(',', $array);
    
$result = doQuery("SELECT Name
                    FROM categories c
                    WHERE c.id IN '" . $id. "'
                    ");
                    
    if(hasRows($result)){
        while($row = mysql_fetch_row($result)){
            echo $row[0];
        }
    }

Upvotes: 1

Views: 216

Answers (2)

Gideon Appoh
Gideon Appoh

Reputation: 663

When you are using IN, you must place the conditions in parenthesis but in you case you are not using them so try something like this:

$array = array(1,3);
$id = implode(',', $array);

$result = doQuery("SELECT Name
                FROM categories c
                WHERE c.id IN (" . $id. ")
                ");

if(hasRows($result)){
    while($row = mysql_fetch_row($result)){
        echo $row[0];
    }
}

Upvotes: 1

Karl Kieninger
Karl Kieninger

Reputation: 9129

I suspect you need your IN wrapped in ( ) not ' '.

$result = doQuery("SELECT Name
                   FROM categories c
                   WHERE c.id IN (" . $id. ")
                    ");

Upvotes: 0

Related Questions