为什么我得到这个错误,一切看起来都正确:“未定义的索引”?

I am at a loss here. I cannot for the life of me figure out why I am getting the error.

Notice: Undefined index: price in /home/*****/public_html/newfinal/home_view.php on line 14

Notice: Undefined index: description in /home/****/public_html/newfinal/home_view.php on line 15

Here are the relevant code snippets:

PHP/ SQL

function get_product($product_id) {
    global $db;
    $query = "SELECT * FROM prjProducts p"
            . " INNER JOIN prjCategories c"
            . " ON p.categoryID = c.categoryID"
            . " WHERE productID = :product_id";
    try {
        $stmt = $db->prepare($query);
        $stmt->bindValue(':product_id', $product_id);
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $stmt->execute();
        $result = $stmt->fetchAll();
        $stmt->closeCursor();
        return $result;
    } catch (PDOException $ex) {
        $error = $ex->getMessage();
        echo $error;
    }
}

PHP

// Set the featured product Id's in an array
$product_ids = array(3, 5, 10);

// Get an array of featured producst from the database
$products = array();
foreach ($product_ids as $product_id) {
    $product = get_product($product_id);
    $products[] = $product; // add the product to the array

}

HTML

<?php foreach ($products as $product) :
          // Get product data
          $price = $product['price'];
          $description = $product['description'];

I have been banging my head at this one for a couple of hours now. I've tried to echo the arrays, and I can print out the arrays without issue. I'm sure it is some small formatting error that I am missing somewhere.

  1. First check whether you have the price and description columns in your table prjProducts.

  2. In your foreach ($products as $product) loop, do a var_dump for $product and check to see whether it is an array or an object?

If it is not an array you should do $product->price instead of $product['price'].