Reputation: 463
I'm relatively inexperienced with php, pdo and sql. that being said, I have been working on a small project and while most things are working as intended i'm having some issues. take the following information from the mysql (mariadb) database
| Field | Type | Null | Key | Default | Extra |
| age | bit(6) | YES | | NULL | |
In the PHP I have a simple form where you enter your age, eg. mine is 33.
$age=decbin($_POST['age']);
this correctly shows 100001 when i echo $age. using my (possibly horrible) pdo statements
$stmt = $DBH->prepare("INSERT INTO Recruit(recruit_id, name, age) VALUES(:recruit_id,:name,:age)");
$stmt->execute(array(':recruit_id' => $recruit_id, ':name' => $name, ':age' => $age));
recruit_id and name both show up without issue, but age always populates as 111111 regardless of what i enter as the age.
I've tried doing cast(:age as binary(6)) with the same results. I'm simply at a loss of how to accomplish this task.
*****corrected typo in the database paste*****
i have changed age to an integer, however, i still yes a couple on/off options that are stored as binary(1) and the same issue presents. php can show the value as a 1 or 0 correctly, but when sent to the db, it is always a 1. (age presented a good example as all 6 bits were 1)
Upvotes: 1
Views: 939
Reputation: 874
UPDATE: After further research, it sounds like my previous answer wont work for the method you are using as parameters passed to the execute
function are all treated as PDO::PARAM_STR
. You'll want to bind them all separately and use PDO::PARAM_INT
for the age variable. Like so:
$stmt = $DBH->prepare("INSERT INTO Recruit(recruit_id, name, age) VALUES(:recruit_id,:name,:age)");
$stmt->bindParam(':recruit_id', $recruit_id, PDO::PARAM_STR);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->bindParam(':age', $age, PDO::PARAM_INT);
$stmt->execute();
(PREVIOUS ANSWER)
From MySQL Reference:
To specify bit values, b'value' notation can be used. value is a binary value written using zeros and ones. For example, b'111' and b'10000000' represent 7 and 128, respectively.
So I'd try this instead: $age = "b'".decbin($_POST['age'])."'";
Upvotes: 1