doudou8783 2014-08-26 14:50
浏览 21
已采纳

too long

I am trying to sum the values from a column using mysqli prepared statement with the code below but is not working. Does anyone can help me pointing what I am doing wrong? Thanks!

$stmt2 = $mysqli->prepare("SELECT SUM(col) as total FROM tb_a WHERE user=?");

$stmt2->bind_param("s", $user);
$stmt2->execute();
$op_row = $stmt2->fetch_assoc();

echo $op_row['total'];
  • 写回答

3条回答 默认 最新

  • dongmi1663 2014-08-26 15:54
    关注

    Give this a go:

    $user = "Larry"; // example
    
    $stmt = $mysqli->prepare("SELECT SUM(col) FROM tb_a WHERE user=?");
        $stmt->bind_param("s", $user);
        $stmt->execute();
        $stmt->bind_result($total);
    
        $stmt->fetch();
    
        echo $total;
    

    or

    $user = "Robert"; // example
    
    $stmt = $mysqli->prepare("SELECT SUM(col) FROM tb_a WHERE user=?");
        $stmt->bind_param("s", $user);
        $stmt->execute();
        $stmt->bind_result($total);
    
        while ($stmt->fetch()) {
    
        echo $total;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?