1

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];
        }
    }
2
  • 1
    After making a DB call, it's often a good idea to check for errors. Also, you should note that you're learning a deprecated library -- consider switching to the newer mysqli or PDO extensions. Commented Jun 18, 2015 at 16:41
  • in requires () to be valid syntax, but depending on your flavor of sql (you didn't specify rdbms) you might need to either use a dynamic sql query, or split your delimited array into a table for in (select id from table) Commented Jun 18, 2015 at 16:44

2 Answers 2

1

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];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.