How to store NULL values in datetime fields in MySQL?

后端 未结 8 498
再見小時候
再見小時候 2020-11-29 05:44

I have a \"bill_date\" field that I want to be blank (NULL) until it\'s been billed, at which point the date will be entered.

I see that MySQL does not like NULL val

相关标签:
8条回答
  • 2020-11-29 06:10

    I just tested in MySQL v5.0.6 and the datetime column accepted null without issue.

    0 讨论(0)
  • 2020-11-29 06:17

    MySQL does allow NULL values for datetime fields. I just tested it:

    mysql> create table datetimetest (testcolumn datetime null default null);
    Query OK, 0 rows affected (0.10 sec)
    
    mysql> insert into datetimetest (testcolumn) values (null);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from datetimetest;
    +------------+
    | testcolumn |
    +------------+
    | NULL       | 
    +------------+
    1 row in set (0.00 sec)
    

    I'm using this version:

    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.0.45    | 
    +-----------+
    1 row in set (0.03 sec)
    

    EDIT #1: I see in your edit that the error message you are getting in PHP indicates that you are passing an empty string (i.e. ''), not null. An empty string is different than null and is not a valid datetime value which is why you are getting that error message. You must pass the special sql keyword null if that's what you mean. Also, don't put any quotes around the word null. See my insert statement above for an example of how to insert null.

    EDIT #2: Are you using PDO? If so, when you bind your null param, make sure to use the [PDO::PARAM_NULL][1] type when binding a null. See the answer to this stackoverflow question on how to properly insert null using PDO.

    0 讨论(0)
  • 2020-11-29 06:19

    I had this problem on windows.

    This is the solution:

    To pass '' for NULL you should disable STRICT_MODE (which is enabled by default on Windows installations)

    BTW It's funny to pass '' for NULL. I don't know why they let this kind of behavior.

    0 讨论(0)
  • 2020-11-29 06:19

    It depends on how you declare your table. NULL would not be allowed in:

    create table MyTable (col1 datetime NOT NULL);
    

    But it would be allowed in:

    create table MyTable (col1 datetime NULL);
    

    The second is the default, so someone must've actively decided that the column should not be nullable.

    0 讨论(0)
  • 2020-11-29 06:21

    I just discovered that MySQL will take null provided the default for the field is null and I write specific if statements and leave off the quotes. This works for update as well.

    if(empty($odate) AND empty($ddate))
    {
      $query2="UPDATE items SET odate=null, ddate=null, istatus='$istatus' WHERE id='$id' ";
    };
    
    0 讨论(0)
  • 2020-11-29 06:30

    Specifically relating to the error you're getting, you can't do something like this in PHP for a nullable field in MySQL:

    $sql = 'INSERT INTO table (col1, col2) VALUES(' . $col1 . ', ' . null . ')';
    

    Because null in PHP will equate to an empty string which is not the same as a NULL value in MysQL. Instead you want to do this:

    $sql = 'INSERT INTO table (col1, col2) VALUES(' . $col1 . ', ' . (is_null($col2) ? 'NULL' : $col2). ')';
    

    Of course you don't have to use is_null but I figure that it demonstrates the point a little better. Probably safer to use empty() or something like that. And if $col2 happens to be a string which you would enclose in double quotes in the query, don't forget not to include those around the 'NULL' string, otherwise it wont work.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题