Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 104389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:16:26+00:00 2026-05-11T01:16:26+00:00

I’m writng a small application in PHP + MySQL and have come to the

  • 0

I’m writng a small application in PHP + MySQL and have come to the point where there is an object that has a couple (8 so far but not expected to increase) of flags associated with it. The flags are pretty much unrelated although there are some combinations that would make no sense. The object represents a row in a DB (has some methods for saving/loading it too) so the question also applies to choosing a storage method.

The question is – how to best represent them both in code and DB? I can think of several ways:

One way to store them in the DB is in a single integer field as bitwise flags. On the PHP side then I can imagine several ways of representing them:

  • Simply export the integer value and define a couple of flag constants; Let every place that needs the flags do their own bitwise magic;
  • Define class methods GetFlag(), SetFlag() and UnsetFlag() that do the bitwise magic on a private integer variable; These methods would then be passed one of the flag constants as a parameters.
  • Define methods GetFlagA(), GetFlagB() etc. (along with Set and Unset counterparts);
  • Define a bunch of member variables that each represent one flag; Set them on loading from the DB and gather them when saving.
  • Create a member variable that is an array of all the flag values. Use predefined constants as array indices to access each flag. Also fill/gather the array on loading/saving.

Another way would be to store them in the DB as separate BIT fields. In PHP that would then translate to several member variables. IMHO this would complicate the queries.

And the last way would be to define anothed table for all the flags and an intermediate table for many-to-many relationship between flags and the original objects. IMHO the messiest of all solutions, considering that there would be only like 3 tables otherwise.

I haven’t done much PHP development so I don’t know what the best practice would be. In C# I would probably store them as bitwise flags and make properties that do the bitwise magic on a private integer. But PHP doesn’t have properties (I’m using latest stable version)…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:16:27+00:00Added an answer on May 11, 2026 at 1:16 am

    In your model, the object has 8 boolean properties. That implies 8 boolean (TINYINT for MySQL) columns in your database table and 8 getter/setter methods in your object. Simple and conventional.

    Rethink your current approach. Imagine what the next guy who has to maintain this thing will be saying.

    CREATE TABLE mytable (myfield BIT(8)); 

    OK, looks like we’re going to have some binary data happening here.

    INSERT INTO mytable VALUES (b'00101000'); 

    Wait, somebody tell me again what each of those 1s and 0s stands for.

    SELECT * FROM mytable; +------------+ | mybitfield | +------------+ | (          |  +------------+ 

    What?

    SELECT * FROM mytable WHERE myfield & b'00101000' = b'00100000'; 

    WTF!? WTF!?

    stabs self in face


    — meanwhile, in an alternate universe where fairies play with unicorns and programmers don’t hate DBAs… —

    SELECT * FROM mytable WHERE field3 = 1 AND field5 = 0; 

    Happiness and sunshine!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 52k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There is no 'make this pretty' function in SQL Server.… May 11, 2026 at 6:36 am
  • added an answer This code should do it in SQL 2005, but a… May 11, 2026 at 6:36 am
  • added an answer You should add the following to the RequiredFieldValidator: enableclientscript='false' Seeing… May 11, 2026 at 6:36 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.