2

I made table with some Boolean fields.

CREATE TABLE PM_ADMIN_LIST(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(250) NOT NULL,
password VARCHAR(250) NOT NULL,
mail VARCHAR(250) NOT NULL,
added_time INT,
super_admin VARCHAR(250) NOT NULL,
last_time INT,
last_ip VARCHAR(250),
see_user_per BOOLEAN DEFAULT FALSE,
change_user_per BOOLEAN DEFAULT FALSE,
see_people_per BOOLEAN DEFAULT FALSE,
change_people_per BOOLEAN DEFAULT FALSE,
add_people_per BOOLEAN DEFAULT FALSE,
remove_people_per BOOLEAN DEFAULT FALSE,
see_album_per BOOLEAN DEFAULT FALSE,
add_album_per BOOLEAN DEFAULT FALSE,
change_album_per BOOLEAN DEFAULT FALSE,
remove_album_per BOOLEAN DEFAULT FALSE,
see_music_per BOOLEAN DEFAULT FALSE,
add_music_per BOOLEAN DEFAULT FALSE,
change_music_per BOOLEAN DEFAULT FALSE,
remove_music_per BOOLEAN DEFAULT FALSE,
admin_per BOOLEAN DEFAULT FALSE,
yahoo_per BOOLEAN DEFAULT FALSE,
status_per BOOLEAN DEFAULT FALSE,
pm_per BOOLEAN DEFAULT FALSE,
ip_blocking_per BOOLEAN DEFAULT FALSE
);

But when I check it in phpmyadmin, BOOLEAN fields are tinyint(1) and they are 0 by default. I can make 1 to be TRUE. But when I use === in my PHP code, they can't be converted. (I should use == in if, for, while and I think it can make some bug in my system.)

So, how should I solve it? How should I get it BOOLEAN by default from mysql?

Olli
  • 752
  • 6
  • 20
Moein Hosseini
  • 4,309
  • 15
  • 68
  • 106
  • 1
    According to http://stackoverflow.com/questions/289727/which-mysql-datatype-to-use-for-storing-boolean-values `BOOLEAN` is just an alias for `TINYINT(1)` - zero values are false, non-zero values are true. Why do you need it to compare it with `===`? PHP should handle conversions of `true/false` to 1/0. – Sebastian Wramba Sep 26 '11 at 11:55
  • 1
    You can't get boolean directly. – N.B. Sep 26 '11 at 11:55
  • @SebastianWramba: I think it will be faster instead of saying php to convert 1 to TRUE . – Moein Hosseini Sep 26 '11 at 12:05
  • This sounds like premature optimization which means: Don't care about the conversion (since it's necessary, see ThiefMaster's answer) and just use `==`. – Sebastian Wramba Sep 26 '11 at 12:11

2 Answers2

12

You usually don't get anything but strings in PHP when fetching data from the database. The types are solely used on the database side.

The field is a TINYINT because MySQL doesn't contain a BOOLEAN field at all.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

There is no 'boolean' type in MySQL. You could just use the == check in PHP.

Rijk
  • 11,032
  • 3
  • 30
  • 45