How to update table with column set NULL in codeigniter
Solution 1
use this to set null column.it will work, dont forget add false on third parameter SET on active record
$this->db->set('id_parent', 'NULL', false);
Solution 2
you can try this code in 'model', hope it'll work:
public function update_std_marks($id_user) {
$this->db->set('id_parent', null);
$this->db->where('id_user', $id_user);
$this->db->update('user');
}
Solution 3
I was curious so I wanted to see how CodeIgniter's Active Record would handle different variations of syntax when working with a null value. I'll map out several test cases for where()
and set()
method calls.
Battery of where()
tests:
-
->where('col', null) // `col` IS NULL
-
->where('col', null, false) // col IS NULL
-
->where('col', 'NULL') // `col` = 'NULL'
-
->where('col', 'NULL', false) // col = NULL
-
->where('col IS NULL') // `col` IS NULL
-
->where('col IS NULL', null) // `col` IS NULL
-
->where('col IS NULL', null, false) // col IS NULL
-
->where('col', 'IS NULL') // `col` 'IS NULL'
-
->where('col', 'IS NULL', false) // col = IS NULL
Properly quoted and formed: #1, #5, #6 (recommended)
Logically formed, but not quoted: #2, #7 (not recommended)
Inappropriate rendered syntax: #3, #4, #8, #9 (do not use)
Battery of set()
tests:
-
->set('col', null) // `col` = NULL
-
->set('col', null, false) // col =
-
->set('col', 'NULL') // `col` = 'NULL'
-
->set('col', 'NULL', false) // col = NULL
-
->set('col IS NULL') // `col IS` `NULL` = ''
-
->set('col IS NULL', null) // `col IS` `NULL` = NULL
-
->set('col IS NULL', null, false) // col IS NULL =
-
->set('col', 'IS NULL') // `col` = 'IS NULL'
-
->set('col', 'IS NULL', false) // col = IS NULL
Properly quoted and formed: #1 (recommended)
Logically formed, but not quoted: #4 (not recommended)
Inappropriate rendered syntax: #2, #3, #5, #6, #7, #8, #9 (do not use)
Solution 4
Your user table must have the attribute set to is_null in the id_parent
field. Otherwise you can't.
The query should be something like
Update table user set id_parent = null where id_user = X
You can set that column nullable with this query:
ALTER TABLE user MODIFY id_parent int(11) null;
Try this for codeigniter:
$fields = array(
'id_parent' => array(
'name' => 'id_parent',
'type' => 'INT',
),
);
$this->dbforge->modify_column('user', $fields);
or simply:
$this->db->query('ALTER TABLE user MODIFY id_parent int(11) null;');
nanasjr
Updated on June 04, 2022Comments
-
nanasjr almost 2 years
i have a problem with update column with set data to NULL, i have a table
user
with each one column isid_parent
column, user can delete parent and can add parent, so ifuser
have aparent
,id_parent
will set with user'sparent id
, but user can delete parent's data, so ifuser
delete parent's data,id_parent
column, will set to NULL. so how to set data to null in database not" "
butNULL
. here's myuser
table.user id_user | name | address | id_parent
-
nanasjr almost 9 yearsso i can user the alter table/? it's same with use active record in codeigniter?