3

I have an issue with PHP and mysqli, as im not really familiar with the coding language i have this issue: When i try to fill the array from row 1 to 8 the only return i get when i do print_r $items the result is 8. Not 1 to 8. Can someone help me ?

Code

$gebruiker = $_SESSION['user'];

$query = "select `item_id` from inventory where `gebruiker_id` = ?";
    $stmt = $db->prepare($query);
    $stmt-> bind_param('i', $gebruiker->id);
    $stmt->execute();
    $stmt->bind_result($item_id);
    $items = array();
    while ($stmt->fetch()) {
        $items['item_id'] = $item_id;
    }

Inventory Table

    gebruiker_id |   item_id
__________________________
    1        |      1
    1        |      2
    1        |      3
    1        |      4
    1        |      5
    1        |      6
    1        |      7
    1        |      8

2 Answers 2

3

You keep overwriting the value of $item['item_id'] in your loop. What you're probably looking for is:

$items[] = $item_id;

That will capture all of the item IDs in an array.

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

5 Comments

Doesn't give me a return at all... EDIT: it did but still it only returns 8
I'm wondering if the OP started the session or not, and if it has any bearing on the issue.
@Fred-ii- Since they do get one row I am assuming their query is running successfully
@JohnConde That's correct, when I run the query trough PHPmyAdmin it returns 8 rows with the correct values.
Gotta be the fetch method then.
2

You are overwriting the same element in the array each time...try:

 $items[] = array('item_id' => $item_id);

11 Comments

Still only returns 8.
Thanks for your help! Although gebruiker_id is a user, so it has to fetch the values for the currently logged in user and the values differs per user, this user has 8 items and another user has 6 items so BETWEEN won't work. when i try to run the query in PHPmyAdmin it returns 8 rows.
Yeah i just noticed that, and deleted the misleading SQL ...I would debug by adding a "count" variable inside of your while loop and making sure that the loop is looping 8 times. It's possible your $stmt->fetch() is not returning the expected number of rows
I tried debugging as you said and it count returns 8 so the loop is working fine.
Alright, for debugging try adding this to the interior of your loop: $items_debug[] = $++count; what is the value of $items_debug after the loop runs?
|

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.