Imagine it like this. There is a field in my database called flags in which are added or removed data like this:
UPDATE people SET flags=flags|16 WHERE ....
UPDATE people SET flags=flags|128 WHERE ....
UPDATE people SET flags=flags&~16 WHERE ....
UPDATE people SET flags=flags&~128 WHERE ....
For instance this field can have value like 65536 or more or less. My question is – How to get specific flag from this field using PHP code? I mean something like this:
SELECT * FROM people WHERE flags=16;
But the result will return all people with not just number 16 in field but it will return people with flag 65536, people with 16 but not people with 2 or 1. Which SELECT query should I use here in my php code or maybe some specific PHP integrated functions? Thank you.
Assuming
flagsis a bitfield and you want to select rows where bit #4 (10000) is setThis is not ideal though as you’re losing out on all that referential goodness that DBs are good for.
What you should have is two new tables;
flagsandpeople_flags. The former contains all the flags (idandnamecolumns should be sufficient). The latter containsflag_idandpeople_idcolumns, creating a many-to-many relationship (see junction table).