Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing specific count query in php

Tags:

php

mysql

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(); ?>
like image 250
Vitalynx Avatar asked Aug 09 '26 16:08

Vitalynx


1 Answers

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'
like image 140
jakub wrona Avatar answered Aug 11 '26 05:08

jakub wrona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!