Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mysqli printing array issue

Tags:

arrays

php

mysqli

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
like image 599
J.Koppen Avatar asked Dec 20 '25 20:12

J.Koppen


1 Answers

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.

like image 169
John Conde Avatar answered Dec 22 '25 11:12

John Conde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!