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.

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

报告相同问题?