我如何解决 php 中的 "Trying to access array offset on value of type int" 错误

how can i solve "Trying to access array offset on value of type int" error in php

我正在使用 MySQL SUM() 函数来获取每件商品的总销量。如何使用 PHP 将 SUM(数量)显示到我的页面?

因为我在执行代码时不断出错。如果你们能给我一些演示代码或文档的例子来解决我的问题,我将不胜感激。谢谢

这些是我的代码

    <?php
  $tsql = "SELECT product_id, SUM(quantity) FROM ordered_items GROUP BY product_id"; 
  $tquery = $conn->query($tsql);

  while($row = mysqli_num_rows($tquery)){
  echo "Total ". $row['product_id']. " = RM". $row['SUM(quantity)'];
  break;
}
?>    

mysqli_num_rows 获取结果中的行数,不能将 ta 数字用作列数组

要获取数组关联数组,您应该使用

 while($row = mysqli_fetch_assoc($tquery ))
{
     echo "Total ". $row['product_id']. " = RM". $row['SUM(quantity)'];
}