13

According to the PHP manual, the four variable types for mysqli->bind_param are

  1. integer,
  2. double,
  3. string and
  4. blob.

What is the best way to insert a boolean?

jww
  • 97,681
  • 90
  • 411
  • 885
fdsa
  • 1,379
  • 1
  • 12
  • 27

1 Answers1

14

MySQL doesn't really store booleans anyway, it's a trick.

The actual format is TINYINT, which is I guess integer for pdo.

You will have to convert true/false to 1/0, with intval for example.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sebas
  • 21,192
  • 9
  • 55
  • 109
  • Is is possible to put a boolean php value directly into the prepared statement, or does it have to be converted? E.g. $test = false - can I insert test as a TINYINT or do I need to first convert $test to an int? – fdsa Jun 24 '12 at 23:09
  • 4
    PHP converts booleans to nothing when you try to put it in a string, so it will not do 1/0 as expected. Just do $booleanvar?1:0 in your prepare call. – Sean Johnson Jun 24 '12 at 23:15