如何通过Query和PHP汇总列的所有行的错误。

时间:2014-03-26 17:22:24

标签: php mysql

这里我试图将一个表中的所有列添加到结果中。

我有一个表已经由用户记录了点,每个点都被添加了我想添加它并打印出来作为用户得分的结果。

运行SUM查询时出错。结果是0。

这是PHP:

<?php
    // see if the form has been completed
    include_once("php_includes/check_login_status.php");
    //include_once("php_includes/db_conx.php");
    // Initialize any variables that the page might echo
    $username = "";
    $weight = "";
    $weighthist = "";
    $id = "";       

    if(isset($_GET["u"])){
        $username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
    } 

    $sql = "SELECT users.*, weighthistory.* FROM users JOIN weighthistory USING(id)";

    $user_query = mysqli_query($db_conx, $sql);


    // check if the user exists in the database
    while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
        $id = $row ["id"];
        $username = $row ["username"];
        $weight = $row["weight"];
        $weighthist = $row["weighthist"];
        $point_hist = $row["point_hist"];

        }

        // this is to calculate points score
        $calweight = $weight - $weighthist;     
        $points = $calweight * 10;

        $result = mysql_query('SELECT SUM (point_hist) AS value_sum FROM points_history'); 
        $row = mysql_fetch_assoc($result); 
        $sum = $row['value_sum'];


?>  

这是HTML:

    <div><b>Point Hist: </b> <?php echo $sum; ?> </div>
    <input id="point_hist" type="hidden" name="point_hist" value="<?php echo $sum; ?>" readonly/>

</html>

1 个答案:

答案 0 :(得分:0)

我设法解决了,感谢这里的提示是我的查询的最终结果等... $res = mysqli_query($db_conx,'SELECT sum(point_hist) FROM points_history'); if (FALSE === $res) die("Select sum failed: ".mysqli_error); $row = mysqli_fetch_row($res); $sum = $row[0];