duanpuchun5275 2015-03-06 17:17
浏览 246
已采纳

MySQL BIT数据类型始终为1

I am facing some unexpected behavior with MySQL BIT datatype. By executing the following PHP code:

$query="INSERT INTO REQUESTS_OFFERS (PROPOSAL,REQUESTS_REQUESTID,PERIOD)
  VALUES ('".$offer."','".$requestid."','".$periodkind."')";    

period column which is of BIT data type is always turning to 1 even if PHP variable $periodkind equals to 0!! I cannot explain that. If trying to change manually 1 to 0 inside MySQL there is no problem...

Do you have any idea why that happens?

Thank you very much

  • 写回答

2条回答 默认 最新

  • doudiao2335 2015-03-06 18:05
    关注

    If your column is a BIT datatype, you don't have to wrap your var into " ' " because it will be interpreted as a string and you want a bit.

    Try to remove single quotes and if it doesn't work, try to cast your $periodkind to a bool like this :

    (bool)$periodkind
    

    ONLY IF MySQL converts false to 0 and true to 1 in BIT datatype

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?