Vitalynx
Vitalynx

Reputation: 984

Implementing specific count query in php

class memberclass {
   function Available()
   {
      if(!$this->DBLogin()) {
          $this->HandleError("Database login failed!");
          return false;
      }

      $ux = $_SESSION['username_of_user'];

      $qry = "Select (one='Not done') + (two='Not done') + (three='Not done') + (four='Not done') + (five='Note done') As num_not_done From $this->tablename Where  username='$ux'";
      $result = mysqli_query($this->connection, $qry);
      $result_length = mysqli_num_rows($result);

      echo "$result_length";
   }
}

I'm trying to show the amount of available items. So for every column where the value is "Not done" for a user it should sum it up in the query to form the total amount of "Not done" items. However when I try to show this number with the following code, I get the value "1" for each user for some reason:

<?= $memberclass->Available(); ?>

Upvotes: 3

Views: 50

Answers (1)

jakub wrona
jakub wrona

Reputation: 2254

You need cast the expressions to INT and then sum them. For MySQL database your query could look like this:

SELECT (CAST(one='Not done' AS UNSIGNED) + 
           CAST(two='Not done' AS UNSIGNED) + 
           CAST(three='Not done' AS UNSIGNED) + 
           CAST(four='Not done' AS UNSIGNED) +
           CAST(five='Not done' AS UNSIGNED)) as num_not_done 
FROM  tableName WHERE username = 'something'

Upvotes: 2

Related Questions