Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO bindParam() and MySQL BIT

Tags:

php

mysql

pdo

I'm trying to update data in a table with a BIT type value in it, like the following :

// $show_contact is either '1' or '0'
$query->bindValue(':scontact', $show_contact, PDO::PARAM_INT);

The problem is, it never changes the value, it remains '1' as set on PHPMyAdmin. I tried different PDO::PARAM_ types without success, everything else is working.

edit full script

        $sql = "UPDATE users SET password = :password, address = :address, postal = :postal, city = :city, contact = :contact, show_contact = :scontact WHERE id = :id";

        $query = $dbh->prepare($sql);

        $query->bindValue(':id', $user->id, PDO::PARAM_INT);
        $query->bindValue(':password', md5($password), PDO::PARAM_STR);
        $query->bindValue(':address', $address, PDO::PARAM_STR);
        $query->bindValue(':postal', $postal, PDO::PARAM_STR);
        $query->bindValue(':city', $city, PDO::PARAM_STR);
        $query->bindValue(':contact', $contact, PDO::PARAM_STR);
        $query->bindValue(':scontact', $show_contact, PDO::PARAM_INT);
        $query->execute();
like image 452
yoda Avatar asked Dec 12 '25 19:12

yoda


2 Answers

PDO has a bit of a bug where any parameter passed to a query, even when specifically given as PDO::PARAM_INT is treated as a string and enclosed with quotes. READ THIS

The only way to tackle it is to try the following:

$show_contact = (int)$show_contact;
$query->bindValue(':scontact', $show_contact, PDO::PARAM_INT);
like image 62
Think Different Avatar answered Dec 14 '25 09:12

Think Different


I believe that the BIT type is mapped to PDO's PARAM_BOOL. Try using it with strictly boolean input.

$show_contact = (bool) $show_contact; // '0' => FALSE, '1' => TRUE
$query->bindValue(':scontact', $show_contact, PDO::PARAM_BOOL);
like image 28
Buga Dániel Avatar answered Dec 14 '25 09:12

Buga Dániel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!