MySQL query sets field to 0 instead of blank string
-
05-07-2019 - |
Question
This one's puzzling me. I have a MySQL query, being run though PDO:
$stmt = $db->prepare( "UPDATE member SET acode='' AND status='active' WHERE username=:u" );
$stmt->bindValue( ':u', $member->username, PDO::PARAM_STR );
$stmt->execute();
The acode
field gets set to 0 for some reason. It was created with
`acode` varchar(8) NOT NULL
Is there something special I need to do when using prepared statements?
Solution
Gidday,
The problem comes with this part of your query:
SET acode='' AND status='active'
the AND turns this into the boolean check of '' AND status='active', which evaluates to 0. Change your query to:
SET acode='', status='active'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow