PHP mysqli print array issue

I have a problem with PHP and mysqli as I am not very familiar with the coding language. I have a problem: when I try to fill an array from rows 1 to 8, I only get one return when I do print_r $items

result 8. Not 1 to 8. Can anyone 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

      

+3


source to share


2 answers


You are rewriting the same element in the array every time ... try:



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

      

+4


source


You are storing the value $item['item_id']

in your loop. You are probably looking for:

$items[] = $item_id;

      



This will capture all the element ids in the array.

+4


source







All Articles