mysql and null values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus

    mysql and null values

    Hello, quick question about MySQL storing NULL values...

    Say I have a textbox called $_POST["text"] and a variable $var.

    if(empty($_POST["text"]))
    $var = NULL;
    else
    $var = $_POST["text"];

    Disregarding filtering/formatting the data, upon inserting $var into
    MySQL, I thought it *should* result in a NULL entry in the database (the
    field is setup to accept NULL values). I am sure $var = NULL before
    inserting into the table because is_null() returns TRUE.

    The field is of type CHAR and the entry gets added simply as ''
    (obviously without the quotes). If I run a select statement to find all
    entries where field = NULL, I get 0, but where field = '' returns the entry.

    I don't understand why it's doing this. The only way I can get it to say
    NULL in the record is by actually setting $var = 'NULL', but that to the
    best of my knowledge is just setting $var to the CHAR value 'NULL', not
    actually NULL.

    Thanks in advance for all your help.

    Marcus
  • lig

    #2
    Re: mysql and null values

    I beleive the main problem is that MySQL has a special syntax when
    setting something to NULL. Simply assigning it will not work. I would
    suggest you look here -
    http://dev.mysql.com/doc/mysql/en/pr...with-null.html . Maybe in
    the PHP you should set it to the string 'NULL' and then in the SQL make
    sure it is not quoted.

    quote from the manual:
    * If you want to search for column values that are NULL, you cannot use
    an
    * expr = NULL test. The following statement returns no rows, because
    * expr = NULL is never true for any expression:
    *
    * mysql> SELECT * FROM my_table WHERE phone = NULL;
    *
    * To look for NULL values, you must use the IS NULL test. The following

    * statements show how to find the NULL phone number and the empty phone
    number:
    *
    * mysql> SELECT * FROM my_table WHERE phone IS NULL;
    * mysql> SELECT * FROM my_table WHERE phone = '';

    Marcus wrote:[color=blue]
    > Hello, quick question about MySQL storing NULL values...
    >
    > Say I have a textbox called $_POST["text"] and a variable $var.
    >
    > if(empty($_POST["text"]))
    > $var = NULL;
    > else
    > $var = $_POST["text"];
    >
    > Disregarding filtering/formatting the data, upon inserting $var into
    > MySQL, I thought it *should* result in a NULL entry in the database[/color]
    (the[color=blue]
    > field is setup to a ccept NULL values). I am sure $var = NULL before
    > inserting into the table because is_null() returns TRUE.
    >
    > The field is of type CHAR and the entry gets added simply as ''
    > (obviously without the quotes). If I run a select statement to find[/color]
    all[color=blue]
    > entries where field = NULL, I get 0, but where field = '' returns the[/color]
    entry.[color=blue]
    >
    > I don't understand why it's doing this. The only way I can get it to[/color]
    say[color=blue]
    > NULL in the record is by actually setting $var = 'NULL', but that to[/color]
    the[color=blue]
    > best of my knowledge is just setting $var to the CHAR value 'NULL',[/color]
    not[color=blue]
    > actually NULL.
    >
    > Thanks in advance for all your help.
    >
    > Marcus[/color]

    Comment

    Working...