0

I'm trying to create a tag function in my webpage and I'm using an array of tags to query the database.

array[tag1, tag2, tag3];

The tags are created in a field

what I'm trying to archive is something like

$query = "SELECT * FROM TABLE WHERE `Tags` = '$tag1' AND '$tag2' AND '$tag3'";

Except with an array, so

$query = "SELECT * FROM TABLE WHERE `Tags` = $array[]";

Thanks

$tag = $_POST['tag'];

    $query = "SELECT * FROM ComicStripTags WHERE `Tag` = '$tag'";
    $result = mysqli_query($link, $query);
    while ($row = mysqli_fetch_array($result)){
        $ID[] = $row['ImageID'];
        print_r ($ID);
    }

BTW I want to use $ID[] to use in another query

7
  • What values does the Tags column of you table contain? Can you list an example? Does it have a comma-separated list of tags per row? Or does each row have only one tag? Commented Oct 10, 2017 at 10:06
  • 1
    The AND clauses in your SQL query probably won't do what you think they do. Could you edit your question to include the structure of the table you're querying, some example values for your array of tags, and the results you'd expect from your table for a few values of those tags? Commented Oct 10, 2017 at 10:06
  • I'm not sure why but when I print_r the $tag array it stacks the values Array ( [0] => 18 ) Array ( [0] => 18 [1] => 19 ) Commented Oct 10, 2017 at 10:21
  • Please share the code snippet to help us understand the problem better? Commented Oct 10, 2017 at 10:23
  • @DeadLock I'll edit the post Commented Oct 10, 2017 at 10:25

1 Answer 1

0

Try using implode to convert array to string, and use "IN" operator instead of equal:

$query = "SELECT * FROM TABLE WHERE `Tags` in (". implode(", ",$array) .")";
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.