0

how do i count punten for each band_naw in php en so the result? i have three dropdowns the first band you select is 3 punten second 2 punten and the third is 1 punten.

here is my code:

/* 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";

} 

my table

enter image description here

error said that column punten not exist but that its strange because i made it

thanks for the comments

1
  • If instead of retuning a useless error message in your else you actually returned the mysqli_error($dbConnection) it would have told you that the query was failing and more importantly WHY Add ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. Commented May 16, 2017 at 11:09

2 Answers 2

3

If you want relative sumarization, you need to group your data with GROUP BY

SELECT band_naw, SUM(punten) AS punten FROM resultaat GROUP BY band_naw
Sign up to request clarification or add additional context in comments.

1 Comment

Still not work it said that the column punten not exist
0

Try this

$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";
}

2 Comments

Still not work it said that the column punten not exist
SHOW CREATE TABLE resultaat. execute this query in your database and post your table schema.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.