1

Let x is a column of the table t allowing NULL values.

Which value should $value variable take for the following PDO statement to insert NULL value?

$db->prepare("INSERT t SET x=:x")->execute(array(':x'=>$value));

Sven
  • 69,403
  • 10
  • 107
  • 109
porton
  • 5,214
  • 11
  • 47
  • 95

2 Answers2

3

You simply insert it as null:

$db->prepare("INSERT t SET x=:x")->execute(array(':x'=>null));

If you want, you can also add the data type of PDO::PARAM_NULL.

SeanWM
  • 16,789
  • 7
  • 51
  • 83
0

This was asked 9 years ago, so it may no longer be relevant, but SeanWM's existing answer, while it works, does not technically answer specifically what was asked because it does not assign any value to a variable.

Just for completeness:

Inserting the PHP null value into MySQL does result in NULL being inserted into the DB.

$value = null;
$sql = $db->prepare("INSERT t SET x=:x")->execute(array(':x'=>$value));

Output:

result of insert query

Note: be careful NOT to enclose the word null in quotes or it will insert the string "null" instead.

snoski
  • 174
  • 11