6

Does anyone know if it is possible to insert NULL into a column with MYSQLI bind_param.

I have a situation where sometimes I want to set a column to null in bind_param. Like so...

$column2 = "NULL";
$insert_data->bind_param('ss', $column1,$column2);

Obviously, this just writes NULL to the column as a string. I've tried null and \0 but they don't work.

I would like to know if there is some value I can make $column2 equal that will cause it to submit and actual null value instead.

Otherwise, I'll have to dynamically build the type and parameter lists and use call_user_func_array to create my binding such that columns aren't written if they are null. This I can do but it leads to a lot of fiddly extra code so I was just wondering if this can be avoided.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Columbo
  • 2,896
  • 7
  • 44
  • 54
  • 4
    Setting `$column2 = null;` should work. What do you mean by "don't work" ? – Victor Nicollet Feb 07 '11 at 11:47
  • That did work. Stupidly I put null between quotes so it was interpreted as the actual string "null". Stared at it for ages and didn't see the quotes. Thanks. If there was one of those smile icon things with a blushing face, I'd be using it now. – Columbo Feb 07 '11 at 11:56

1 Answers1

16

use: $column2 = null; not: $column2 = "null";

animuson
  • 53,861
  • 28
  • 137
  • 147
Columbo
  • 2,896
  • 7
  • 44
  • 54