0

I have a tags_ids array 1,3,2 and

data in my table:

+---------+----------+
| user_id | tag_id   |
+---------+----------+
|       1 |        1 |
|       1 |        2 |
|       2 |        1 |
|       2 |        2 |
+---------+----------+

I want to get the users ids into array, but not working:

foreach ($tags_ids as $i)
    {
        if ($result = $mysqli->prepare("SELECT `user_id` FROM `mytable` WHERE `tag_id`=?"))
        {
            $result->bind_param("i",$i);
            $result->execute();
            $result->bind_result($d);
            $result->fetch();
            $result->close();
        }

        if (!in_array($d,$users_ids)) $users_ids[] = $d;
    }

My result is always 1. Whats I'm doing wrong, and can I do it in a more simple way?

2
  • 3
    You need to call fetch() in a loop to get all rows. A single call to fetch() only retrieves one row. Commented Dec 21, 2013 at 15:00
  • 2
    See examples in the mysqli::fetch() documentation Something like while($result->fetch()) { $users_ids[] = $d; } Commented Dec 21, 2013 at 15:00

1 Answer 1

1

you need a while loop, you can find it here for full assist:http://www.youtube.com/watch?v=hO0YOOeJrOE
be sure to watch the other video's too, very helpfull.

goodluck, PHPNoob

Sign up to request clarification or add additional context in comments.

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.