我是 php 新手,我正在尝试从我的数据库产品表中获取产品名称并将该数据存储到 $product 数组中。我收到错误
count():参数必须是数组或对象,在 C:\xampp\htdocs\moed\cart.php 第 57 行实现 Countable
$sqlprod = "SELECT pname FROM products limit 10";
$sqlprice = "SELECT price FROM products limit 10";
$result = $conn->query($sqlprod);
$result1 = $conn->query($sqlprice);
if ($result->num_rows And $result1->num_rows > 0) {
$products = array();
$amounts = array();
while($row = mysql_fetch_assoc($sqlprod)){
$products[] = $row;
}
while($row1 = mysql_fetch_assoc($sqlprice)){
// add each row returned into an array
$products[] = $row1;
}
}
// I am getting error of undefined variable products at the for loop below
if ( !isset($_SESSION["total"]) ) {
$_SESSION["total"] = 0;
for ($i=0; $i< count($products); $i++) {
$_SESSION["qty"][$i] = 0;
$_SESSION["amounts"][$i] = 0;
}
}