本文介绍了PHP PDO bindParam() 和 MySQL BIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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);

问题是,它永远不会改变值,它仍然是 PHPMyAdmin 上设置的1".我尝试了不同的 PDO::PARAM_ 类型但没有成功,其他一切正常.

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.

编辑完整脚本

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

推荐答案

PDO 有一个错误,即任何传递给查询的参数,即使特别指定为 PDO::PARAM_INT 也被视为字符串并用引号括起来.阅读本文

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

这篇关于PHP PDO bindParam() 和 MySQL BIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:15