1

I want to check if any user in my Database has in categorie_bit a value of 0 or 1 and check this before my foreach.

I got this in my php script:

foreach ($database->query('SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1') as $resultRandom) {
    var_dump($resultRandom);
}

Pseudocode( I want something like this):

if(categorie_bit = 1) {
    foreach ($database->query('SELECT id,firstname,lastname FROM `staff` WHERE categorie_bit = 1') as $resultRandom) {
        var_dump($resultRandom);
    }
} else {
      echo "0";
}
5
  • I do not know what you mean Commented Sep 9, 2015 at 8:45
  • Not sure your pseudo code makes any sense. Could you describe in english what you want to do instead? Commented Sep 9, 2015 at 8:45
  • Sure, I want to check if any user in my Database has in categorie_bit a value of 0 or 1 before my foreach. Commented Sep 9, 2015 at 8:50
  • You cannot check the value of a column on a database table before you have read the row from the table Again makes no sense Commented Sep 9, 2015 at 8:51
  • So I have to put the condition inside my foreach and check if categoire_bit is 1 or 0 Commented Sep 9, 2015 at 8:52

2 Answers 2

1

You can achieve the same thing by iterating using a while loop instead of a foreach. At each iteration the $row variable is the row from the table:

$result = $database->query('SELECT * FROM `staff`');

while($row = $result->fetch_array(MYSQLI_ASSOC)) { 

    if($row['categorie_bit'] == 0) {
        //Do something for 0's
    }    
    if($row['categorie_bit'] == 1) {
        //Do something for 1's
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is probably what the OP wants to do but just in case the category bit can have other values I changed the code a bit. I hope you dont mind.
Your guess is as good as mine! Good point regarding other possible values, I was just assuming either 1's or 0's. Thanks!
0

"Sure, I want to check if any user in my Database has in categorie_bit a value of 0 or 1 before my foreach"

How about in your SQL

... WHERE categorie_bit = 1 OR categorie_bit = 0

or, this checks whether categorie_bit is set (assuming it can be 0, 1 or NULL)

WHERE categorie_bit IS NOT NULL

Comments

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.