PHP PDO bindParam() and MySQL BIT

一世执手 提交于 2021-02-04 20:51:32

问题


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();

回答1:


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);



回答2:


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);


来源:https://stackoverflow.com/questions/24326283/php-pdo-bindparam-and-mysql-bit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!