mysqli_fetch_array()期望参数1为mysqli_result-需要帮助

时间:2019-03-19 07:59:21

标签: php

mysqli_fetch_array()期望参数1为mysqli_result,布尔值为-

$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result=mysqli_query($db, $query);


while($row = mysqli_fetch_array($result)){
    echo "Total ". $row['type']. " = $". $row['SUM(price)'];

}

2 个答案:

答案 0 :(得分:1)

首先,检查$db标识符,在哪里定义连接?如果连接正常,则需要使用mysqli_error()

检查查询错误

您应该检查$result参数:

$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result=mysqli_query($db, $query);

if($result === FALSE) { 
    printf("Error: %s\n", mysqli_error($db)); // TODO: better error handling
}    

while($row = mysqli_fetch_array($result)){
    echo "Total ". $row['type']. " = $". $row['SUM(price)'];

}

答案 1 :(得分:-1)

使用“为”:

$query = "SELECT type, SUM(price) as sums FROM products GROUP BY type";
$result=mysqli_query($db, $query);
while($row = mysqli_fetch_array($result))
{
echo "Total ". $row['type']. " = $". $row['sums'];
}