What happens when I send boolean True-False to a PDO statement as a parameter which is bound to an int field?
12,885
Solution 1
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);
Solution 2
The equivalents get passed:
True = 1
False = 0
Comments
-
Uğur Gümüşhan almost 2 years
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.
-
Dom over 7 yearsThis doesn't seem to be true in PHP 7. Whilst true equates to 1, passing false give the error: Incorrect integer value
-
Admin about 6 yearsPassing 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.