Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: vladefix - marraskuu 16, 2013, 05:48:01 AP

Otsikko: Insert null value into db
Kirjoitti: vladefix - marraskuu 16, 2013, 05:48:01 AP
Hy,
is there a way to update a column to a null value in a statement like the folowing :


$smcFunc['db_query']('', "
   UPDATE `{db_prefix}mytable`
      SET
         `name` = {string:name},
         `fk_dummy` = {int:id_dummy}
     WHERE
         `id` = {int:id}",
         array('name' => 'test', 'id_dummy' => '', 'id' => 1));

the column is a foreign key so i can't just insert 0 or -1

Thanks
Otsikko: Re: Insert null value into db
Kirjoitti: emanuele - marraskuu 16, 2013, 06:09:52 AP
Probably something like:
$smcFunc['db_query']('', "
   UPDATE `{db_prefix}mytable`
      SET
         `name` = {string:name},
         `fk_dummy` = {raw:id_dummy}
     WHERE
         `id` = {int:id}",
         array('name' => 'test', 'id_dummy' => 'null', 'id' => 1));
Otsikko: Re: Insert null value into db
Kirjoitti: vladefix - marraskuu 17, 2013, 04:40:28 AP
Lainaus käyttäjältä: emanuele - marraskuu 16, 2013, 06:09:52 AP
Probably something like:
$smcFunc['db_query']('', "
   UPDATE `{db_prefix}mytable`
      SET
         `name` = {string:name},
         `fk_dummy` = {raw:id_dummy}
     WHERE
         `id` = {int:id}",
         array('name' => 'test', 'id_dummy' => 'null', 'id' => 1));


that worked
thank you
Otsikko: Re: Insert null value into db
Kirjoitti: Arantor - marraskuu 17, 2013, 11:10:38 AP
Btw, SMF doesn't make this easier because SMF doesn't use null itself in any column, and there is actually a performance saving at the database level if a table has no null columns.