简体   繁体   中英

PHP mysqli empty array

I have a question about my code. When i try to fill my html with my fetched array i only get blank spaces instead of the actual results. Can anyone tell me why ?

<?php
require_once("php/loader.inc.php");
include("php/header.php");
include("php/userpanel.php");
$gebruiker = $_SESSION['user'];
?>


<div class="large-8 columns">
<div class="row">  


<?php
$query = "
SELECT
   items.naam,
   items.prijs,
   items.image_url
FROM inventory
JOIN items
 ON items.id = inventory.item_id
WHERE
 gebruiker_id = ?
";

$stmt = $db->prepare($query);
$stmt-> bind_param('i', $gebruiker->id);

$stmt->execute();

$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
    $user_items[] = $row;

   echo '<div class="large-3 small-4 columns">
<img src="'. $user_items['image_url'] .'">
<div class="panel">
    <h6>'. $user_items['naam'] .'</h6>
<h6 class="subheader">'. $user_items['prijs'] .'</h6>
</div>
</div>'; 

}
?>

By the way, when i try print_r($user_items); it gives me the expected result.

Thanks in advance!

I think you want to change this:

$user_items[] = $row;

to this:

$user_items = $row;

Otherwise your indexes are 1 dimension 'deeper'

As an example:

$row = array("image_url" => "xy");

after this line:

$user_items[] = $row;

You can't access it with:

echo $user_items["image_url"];  //Wrong

you would have to do this:

echo $user_items[0]["image_url"];  //works

You don't need this line $user_items[] = $row; you can use $row directly

while ($row = $result->fetch_assoc())
{        
   echo '<div class="large-3 small-4 columns">
  <img src="'. $row['image_url'] .'">
  <div class="panel">
    <h6>'. $row['naam'] .'</h6>
    <h6 class="subheader">'. $row['prijs'] .'</h6>
  </div>
  </div>';     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM