7

I have an int field in database and :disabled is supposed to be true false, I am assuming database gets boolean values as integer 0 and 1, but I am unsure.

function loadbyinput($name,$password,$ipnumber="0.0.0.0",$type="member",$disabled=FALSE){    
$dbh = new PDO(...);
$statement=$dbh->prepare("insert into 
 actor(name,password,ipnumber,type,disabled)
 values(:name,:password,:ipnumber,:type,:disabled)");
$statement->bindParam(":disabled", $disabled);
}

I am not writing any GUI at the moment so it is hard to test such things for me.

2
  • 1
    you don't need a gui to test this. you have the code right there - just run it and see what happens.
    – Tim G
    Dec 5, 2011 at 23:05
  • @TimG not that easy, it is a part of a class, to test, I need to create an instance somewhere, and lines of codes... Dec 5, 2011 at 23:19

2 Answers 2

11

Depends on your schema. For boolean columns in the database you can use the following construct (there is a BOOLEAN construct, but it's just an alias for TINYINT):

`disabled` tinyint(1) NOT NULL DEFAULT '0'

Then when you bind, you can enforce a bool value:

$stmt->bindValue(':disabled', $disabled, PDO::PARAM_BOOL);
0
4

The equivalents get passed:

True = 1
False = 0
2
  • 19
    This doesn't seem to be true in PHP 7. Whilst true equates to 1, passing false give the error: Incorrect integer value
    – Dom
    Nov 20, 2016 at 16:58
  • 7
    Passing a bool(false) value via an array into PDO and mariadb that for a column that has the tinyint(1) type in PHP7 can cause an error being thrown. It can be avoided by casting the boolean value to an int. Tricky is that bool(true) values pass through without an error being thrown. I honestly think this is a bug in the PHP/PDO/mysql interface somewhere.
    – user3277192
    Mar 20, 2018 at 19:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.