19

I want to send a value of "NULL" to a column in mySQL.

   $ParentEventID = "NULL";

   mysql_query("UPDATE events SET 
     ParentEventID = '$ParentEventID'");

The column keeps defaulting to "0". I have the column set to accept NULL and I can edit the cell with the NULL check box.

I need to not set the default to NULL because it may effect code in other places.

1
  • 1
    +1 For a question that seems like a FAQ but I've never seen it on SO before. Commented Jun 18, 2011 at 20:55

1 Answer 1

22

You are on the right track making "NULL" a string. If you need to set it NULL, then remove the qoutes around $ParentEventID.

mysql_query("UPDATE events SET ParentEventID = $ParentEventID");

However, before doing so make sure that the value of $ParentEventID === NULL.

Sign up to request clarification or add additional context in comments.

4 Comments

Great! That did it. So...by removing the quotes what exactly is happening?-OK I see quotes make it a string. Thanks guys. I will accept your answer when SO lets me-in 10 minutes
What is happening is that null in SQL is not the string "NULL", but the keyword NULL. So you put that, without quotes, into the query. Just the same way you don't put quotes around the UPDATE keyword. Remember, you're not really "sending" anything: you're just forming an SQL query in a PHP string.
@Joel by removing the single-quotes, the string NULL is passed in as the MySQL keyword NULL. If you keep the single-quotes as 'NULL', MySQL interprets it as a string value. If it is going into an integer column or a boolean column, I guess that's being interpreted as the value '0'.
If you're expecting a possibility of other values it's a good idea to have quotes for everything other than NULL, even though it's a numeric field. (to prevent SQL injection)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.