如何将mysql输出到php变量?

时间:2017-11-14 08:40:14

标签: php mysql sql

这里我有两个变量叫做$ semresult,$ examresult,我需要存储两个相应查询的结果。然后我需要用它们进行计算。我到目前为止尝试的代码是

  $output = '';  
  $connect = mysqli_connect("localhost", "root", "", "crudnew");  

        $semfee = "select sum(amount) from semesterfees";
        $semresult = mysqli_query($connect,$semfee);

        $examfee = "select sum(amount) from examfees";
        $examresult = mysqli_query($connect,$examfee);

        $income = ((int)$semresult + (int)$examresult);

但每当我运行上面的代码$ semresult时,$ examresult会得到错误"类mysqli_result的对象无法转换为int"。请帮忙:')

2 个答案:

答案 0 :(得分:0)

$semfee = "select sum(amount) as total from semesterfees";
$result = mysqli_query($connect,$semfee);

$row = mysqli_fetch_assoc($result);

$semresult = $row['total'];

然后你为第二个查询做同样的事情。

您可以在此处详细了解mysqli_fetch_assoc及其作用。

答案 1 :(得分:0)

您正在传递无效查询。请检查一下这个

    $semfee = "select sum(amount) from semesterfees";
    $semresult = mysqli_query($connect,$semfee );

    $examfee = "select sum(amount) from examfees";
    $examresult = mysqli_query($connect,$examfee);

    $income = ((int)$semresult['amount'] + (int)$examresult['amount']);