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

Matt. C
  • 9
  • 4
  • 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? – DeadLock Oct 10 '17 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? – ymbirtt Oct 10 '17 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 ) – Matt. C Oct 10 '17 at 10:21
  • Please share the code snippet to help us understand the problem better? – DeadLock Oct 10 '17 at 10:23
  • @DeadLock I'll edit the post – Matt. C Oct 10 '17 at 10:25
  • @Matt.C do the print_r outside the while loop to print all the final values obtained from the query only once. – DeadLock Oct 10 '17 at 10:30
  • @DeadLock Thanks, that cleared it up – Matt. C Oct 10 '17 at 10:36

1 Answers1

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) .")";
Lamar
  • 1,761
  • 4
  • 24
  • 50