我有一个{$ obj-> product_stock},其中包含一个数值 - 产品的库存。
如何将{$ obj-> product_stock}转换为SESSION?这是我试过的......
Two
为什么会出错?如何绕过这个?
$_SESSION['product_stock'] = {$obj->product_stock};
答案 0 :(得分:1)
您的代码:
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
在while循环之外。因此,$obj
尚未定义。
将上面的代码移动到while循环中,如下所示
$products_item = '<ul class="products">';
// Fetch results, set as object and output HTML
while($obj = $results->fetch_object()) {
$_SESSION['product_stock'] = $obj->product_stock;
echo "STOCK: ". $_SESSION['product_stock'] ."";
....