3
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(); ?>
6
  • have you heared about SUM() and GROUP BY? Commented Oct 15, 2016 at 18:11
  • @jakubwrona I have tried "Select SUM((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'"; ` But it still gives the same result Commented Oct 15, 2016 at 18:17
  • 1
    I didn't mean this... could you provide an example of data from the table you are querying? Commented Oct 15, 2016 at 19:43
  • @jakubwrona link So what I want the code to do is count the amount of times it says "Not done" for a user (so that row). So then I can implement it in HTML as a statistic. Tasks not completed: <? php code here ?> For the row in the link I sent you it would say: Tasks not completed: 5. Commented Oct 15, 2016 at 20:11
  • Why do you store strings instead of 1 or 0 for DONE or NOT DONE ? This problem is about how you store the data, not about query complexity. Commented Oct 15, 2016 at 22:50

1 Answer 1

2

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'
Sign up to request clarification or add additional context in comments.

2 Comments

$ux = $_SESSION['username_of_user']; $qry = "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 $this->tablename WHERE username = '$ux'"; $result = mysqli_query($this->connection, $qry); $result_length = mysqli_num_rows($result); echo "$result_length"; I still get the result "1" for each user, your code sounds logical but it just doesn't seem to work. All I know is that the answer lies in the query.
the number of rows will be one, but the only row will contain an index called "num_not_done" which will contain the right number. So do a fetch_array and then display the $fetched[0]['num_dot_done']

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.