15

Currently I'm using Tinyint(1) to indicate Boolean values in my MySQL databases, which I really don't like that. So, how could I store and retrieve Boolean values in my MySQL databases via PHP?

How to use it in WHERE clause and how to assign the value in INSERT, UPDATE queries properly?

When I have it back on PHP, it's TRUE, true, or simply 1, if I'm gonna check that with ===?

Also did you ever had any problem when you migrating from Tinyint(1) to BOOLEAN?

Thanks in advance. :)

Update:

I know that Tinyint(1) is the same as Boolean, however I want to work on Boolean data type instead of Tinyint(1). That's why I'm asking the question.

3
  • 7
    BOOL, BOOLEAN are only synonyms for TINYINT(1) in MySQL. Commented Sep 30, 2012 at 11:10
  • 1
    thanks, yes I know that, but I want to work on Boolean in my PHP side, instead of Tinyint, that's why I'm asking the question ... Commented Sep 30, 2012 at 11:13
  • Though it depends on your methodology, you may need to cast the true false keywords to int when inserting into any int type. I do believe that some forms of prepared statements make this arrangement for you, but if your query is inline, then this is necessary. Commented Feb 10, 2017 at 20:01

3 Answers 3

25

MySQL doesn't have a boolean data type. Tinyint(1) is pretty close enough. Working with this in PHP is simple.

If (1) echo 'true'; // is the same as if (true)
// Just as
if (0) echo 'false'; // is the same as if (false)

And if you really really want a boolean value, you can do

// $mysql_data is tinyint value from db
$boolean = $mysql_data ? true : false;
// Now you have your boolean as $boolean
Sign up to request clarification or add additional context in comments.

1 Comment

In MySQL, the values TRUE and FALSE are merely aliases for 1 and 0 as explained in mysql docs
4

With booleans, don't use === FALSE - the value is already a boolean (unless the function requires you to use ===, like strpos()). The value is booleanish - it's technically an integer, but PHP is a dynamic language, so it its not a problem.

Consider preg_match() function - it returns the number of matches (integer).

Would you prefer to write that?

if (preg_match('/\bregexp?\b/', $variable) === 1)

Or that?

if (preg_match('/\bregexp?\b/', $variable))

Obviously, the way without explicit === 1 is better. You ask if it matches, not if it has 0 matches. Also, if you think that === 1 is safer, why not do === 1 === TRUE?

Of course, it's possible to convert values to booleans using (bool) or !!.

Also, in certain languages such as C or Perl, there is no difference between booleans and numbers. It just works.

Comments

0
<select name="can_get" id="can_get" class="form-control">
                <option <?php if($can_get== "1"){ echo "selected"; } ?> 
value="1">True</option>
                <option <?php if($can_get== "0"){ echo "selected"; } ?> 
value="0">False</option>
            </select>

2 Comments

I use this It is working fine
OP already knows that he can use the integer to determine true or false in his PHP code. I don't think your answer adds anything new to this post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.