PHP mysql PDO refuses to set NULL value

后端 未结 2 1812
無奈伤痛
無奈伤痛 2020-12-30 07:08

I am unable to set a nullable field with a default value of null to null using mysql pdo. I can do it using straight sql.

I have tried: (mostly from this question Ho

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 07:23

    The following works for me:

    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    $stmt = $pdo->prepare("INSERT INTO `null_test` (`can_be_null`) VALUES (:null)");
    $stmt->bindValue(":null", null, PDO::PARAM_NULL);
    
    $stmt->execute();
    

    Pass in PHP's null, with type of PDO::PARAM_NULL. Also, make sure your prepare emulation is set to false. That might help.

提交回复
热议问题