php mysqli查询如果我添加总和或计数它doenst工作

时间:2017-05-16 10:57:14

标签: php mysqli

我如何计算php en中每个band_naw的punten所以结果如何? 我有三个下拉列表,你选择的第一个乐队是3个小节,第二个是2个小节,第三个是1个小节。

这是我的代码:

/* Change database details according to your database */
$dbConnection = mysqli_connect('localhost', 'root', '', 'popgroep');

$query  = "SELECT band_naw, SUM(punten) AS punten FROM `resultaat` ";
$result = mysqli_query($dbConnection, $query);

if (mysqli_num_rows($result) > 0) {

    echo "<ul>";

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

        echo "<li>{$row['band_naw']} {$row['punten']}</li>";

    }

    echo "</ul>";

} else {

    echo "Query didn't return any result";

} 

我的桌子

enter image description here

错误说栏目不会存在,但是它很奇怪,因为我做到了

感谢您的评论

2 个答案:

答案 0 :(得分:3)

如果您需要相对sumarization,则需要使用GROUP BY

对数据进行分组
public static boolean copyFile(InputStream inputStream, OutputStream out) {
    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        out.close();
        inputStream.close();
    } catch (IOException e) {
        Log.d(TAG, e.toString());
        return false;
    }
    return true;
}

答案 1 :(得分:0)

试试这个

$con = mysqli_connect("localhost", "root", "", "testing");

// Check connection

if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT band_naw, SUM(punten) AS punten FROM band GROUP BY band_naw";
$result = mysqli_query($con, $query);

if (mysqli_num_rows($result) > 0)
{
    echo "<ul>";
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
        echo "<li>".$row['band_naw']." ".$row['punten']."</li>";
    }

    echo "</ul>";
}
else
{
    echo "No data found";
}