0

I am working on the following snippet. How can I load all affected rows into $items array?

As you can see I am able to fetch each binded cell like $pid and $psku but I need to load them to $items

$items =[];
$stmt = $conn -> prepare("SELECT `pid`,`psku` FROM `appolo` ORDER BY `pid` ASC LIMIT 24");

$stmt -> execute();
$stmt -> store_result();
$stmt -> bind_result($pid, $psku);

while ($stmt -> fetch()) {
    echo $pid;
    echo $psku;
}
$stmt->free_result(); 

echo json_encode($items);
2
  • 1
    Use array_push instead of echoing them? Commented Apr 10, 2019 at 19:41
  • So what's the problem - [] notation already invented. Commented Apr 10, 2019 at 19:41

1 Answer 1

1

It's pretty simple, just build an array of the variables and add to an array:

while ($stmt -> fetch()) {
    $items[] = array($pid, $psku);
}

To get an associative array:

    $items[] = compact('pid', 'psku');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks AbraCadaver this was exactly what I was looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.