dongwuwan5646 2014-05-18 13:42
浏览 173
已采纳

SQL在PHP中将SUM作为INT返回

I have a SQL query that returns SUM, and I need to use that SUM later on to sum it to another field in PHP, so I could return the TotalSum (old points + new points) to the database, but I get the following error:

Object of class mysqli_result could not be converted to int

It doesn't add these values that I fetch from database.

My code is:

$Sum1= mysqli_query($con, "SELECT SUM(`Points`) FROM `table` WHERE `Id`=$ID ");
$Sum2= mysqli_query($con, "SELECT SUM(`PointsTwo`) FROM `table` WHERE `Id`=$ID");

$TotalSum=$Sum1+ $Sum2+ $NewPoints+ $NewPointsTwo;

I would like to point out that values in the columns are integers

  • 写回答

1条回答 默认 最新

  • dqiaw48488 2014-05-18 13:47
    关注

    You need to first fetch the result of your query like so:

    $Sum1= mysqli_query($con, "SELECT SUM(`Points`) FROM `table` WHERE `Id`=$ID ");
    $row = mysqli_fetch_array($Sum1);
    
    // $row now contains all the column values for the query
    $TotalSum = $row[0] + ...
    

    Check out this tutorial for more info on using the mysqli functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作