Mont
Mont

Reputation: 51

What's the best boolean value for MySQL?

Having issues with the value being returned from MySQL from an AJAX query that selects a check box. But the only way I can get it to work is to have the value in MySQL as NULL.

But 0 or 1 will return it checked every time. In MySQL I have it set to tinyint(1).

Upvotes: 3

Views: 2584

Answers (5)

murilo ramos carraro
murilo ramos carraro

Reputation: 23

MySQL uses TINYINT(1) for the SQL BOOL/BOOLEAN. So I would use BOOLEAN, in accordance to standard SQL.enter link description here

Upvotes: 1

Johnny000
Johnny000

Reputation: 2104

Use boolean. Using bit(1) is the wrong approach.

As you can read here, a field assigned as bit(1) could also be i.e. 11111111, so much more data then a boolean/tinyint field could handle.

Upvotes: 0

Johan
Johan

Reputation: 76753

In MySQL boolean is an alias for tinyint(1). I suggest using boolean.
The MySQL writers do things for a reason. I'm sure they have a good reason for choosing tinyint over bit.

You will never get smaller storage requirement than 1 byte.

Make sure you do a check against 1

if $row['bool_field'] == 1 {check that box}
else {uncheck that box}

Upvotes: 7

declension
declension

Reputation: 4185

I think you want a Boolean value here - it's what they're designed for after all. Either way, it should definitely be defined as NOT NULL; most boolean logic situations will have unwanted edge-cases unless you do so (Ternary logic aside!).

Hazarding a guess, perhaps your view code to populate the check box is checking for the existence of a key rather than that value being False (or 0), or the database query is doing a join improperly for this situation. Hard to tell without more detail.

Upvotes: 1

Icarus
Icarus

Reputation: 63970

Try using bit(1) as your data type http://dev.mysql.com/doc/refman/5.0/en/bit-field-values.html

Upvotes: 0

Related Questions