douzhenggui8171 2014-06-20 11:30
浏览 57
已采纳

PHP PDO bindParam()和MySQL BIT

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();
  • 写回答

2条回答 默认 最新

  • dongleiwei2182 2014-06-20 11:36
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?