如何使用PHP显示SUM(行)的值?

I'm using PHP to fetch all values from a column in MySQL database and echo the total number with all of them combined, with a WHERE clause. But I get no value displayed.

My database looks like this:

name|status|rows|
Mike|Klar|100|
Pete|Oklar|50|
Anna|OKlar|50|
Kirk|Klar|30|

My goal is to have "100" printed. (50+50)

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT SUM(rows) FROM registervard WHERE NOT status IS 'Klar'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Rows: " . $row["rows"]. " <br>";
    }
} else {
    echo "0 results";
}
$conn->close();

I expect "100", but the page is blank. If I remove the WHERE clause I get "0 results"

If you want use where status is not klar. Change your code to this...

$sql = "SELECT SUM(rows) FROM registervard WHERE  status != 'Klar'"; 

There's two issues: Your where is clunky, and you're not getting the right column name when you fetch the array. If you change the query to this to fix the WHERE and use an alias for the SUM

SELECT SUM(`rows`) as 'rows' FROM registervard WHERE `status` != 'Klar'

You'll be able to get the result

Let me help you fix your code and hopefully teach you few tricks.

  • In MySQL you can only use the IS or IS NOT operator to compare against booleans, i.e. TRUE, FALSE, or UNKNOWN. - MySQL docs

  • To compare against a string value or any other value you can use the not-equals operator !=

  • You should also enable MySQLi exceptions to see the error messages if there are any. Use mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); before opening connection.

  • Check against num_rows in your code is also wrong because SELECT SUM(...) ... will always give you 1 row even if there were no records matched.

Consider this fixed version of your code:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT SUM(`rows`) AS sumOfRows FROM registervard WHERE status != 'Klar'";
$result = $conn->query($sql);

$row = $result->fetch_assoc();
if (isset($row["sumOfRows"])) {
    echo "Rows: " . $row["sumOfRows"]. " <br>";
} else {
    echo "0 results";
}
  • First line enables exception mode. You do not need to check for connection errors manually anymore.
  • In you query I changed WHERE NOT status IS 'Klar' to WHERE status != 'Klar'.
  • If you ever decide to use MariaDB, then remember that rows is now a reserved keyword, so you have to enclose it with backticks like `rows`
  • You could use $row["SUM(`rows`)"], but if you use alias it will make your code simpler. Add alias to the sum like this SUM(`rows`) AS sumOfRows and then you can access it like $row["sumOfRows"]
  • There is no need to loop on the results, because sum will always be returned as a single row unless you use aggregation.
  • To check if there were any rows matched at all use isset($row["sumOfRows"]). This makes a distinction between a sum of 0 and no records matched.