PDOstatement (MySQL): inserting value 0 into a bit(1) field results in 1 written in table

后端 未结 4 993
醉酒成梦
醉酒成梦 2020-11-30 05:47

I\'m using a bit(1) field to store boolean values and writing into the table using PDO prepared statements.

This is the test table:

CREATE TABLE IF N         


        
4条回答
  •  难免孤独
    2020-11-30 06:21

    pdo by default doesnt use prepared statements for the mysql driver, it emulates them by creating dynamic sql behind the scenes for you. The sql sent to mysql ends up being a single quoted 0 like '0', which mysql interprets as a string, not a number.

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    

    It should work now, and you also will be actually using real prepared statements.

提交回复
热议问题