3

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).

informatik01
  • 16,038
  • 10
  • 74
  • 104
Mont
  • 51
  • 10
  • It seems that bit is a better data type for this – Icarus Oct 25 '11 at 21:36
  • 1
    There's probably a bug in your code. It might help if you posted the code. – Mark Byers Oct 25 '11 at 21:36
  • I don't think your issue is in the choice of data types here. You would need to post your query and what you're doing with the data for help on that side of things. However, to answer your question tinyint(1) might be substantially more concise than something like an enum. It kind of depends on what you're really doing though. I use enums for readability since I tend to work on smaller load sites. My coworkers prefer tinyint(1) because it is smaller in data and is portable. If the site is small and not heavily trafficked then it won't slow anything down to use an enum. Your choice. – Kai Qing Oct 25 '11 at 21:39
  • I've tryed a few different things on this now. Here's the first issue I've been trying to solve. [link]http://stackoverflow.com/questions/7881998/jquery-autocomplete-return-a-checkbox-checked The reason I'm trying to find out about the mysql boolean value is with the code from the link above, the ONLY way I've been able to get the check box to not be checked is with a _NULL_ value in mysql. I've been going around in circles on this all day. – Mont Oct 25 '11 at 21:45

5 Answers5

7

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}
Johan
  • 74,508
  • 24
  • 191
  • 319
1

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

1

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.

declension
  • 4,110
  • 22
  • 25
  • I posted a link to the actual problem I'm having with it. It's # 7881998. I do get the proper values back ie: 0 or 1. But they mark the check boxes with the check not matter what it uses the 0 or 1 as the true value, only when then mysql field if _NULL_ will it not check the box. – Mont Oct 26 '11 at 13:38
0

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.

Johnny000
  • 2,058
  • 5
  • 30
  • 59
0

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

Icarus
  • 63,293
  • 14
  • 100
  • 115