3

i have a problem with update column with set data to NULL, i have a table user with each one column is id_parent column, user can delete parent and can add parent, so if user have a parent, id_parent will set with user's parent id , but user can delete parent's data, so if user delete parent's data, id_parent column, will set to NULL. so how to set data to null in database not " " but NULL. here's my user table.

user

id_user | name | address | id_parent
0

4 Answers 4

6

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);
Sign up to request clarification or add additional context in comments.

Comments

5

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');
}

Comments

4

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.

The examples below are sorted by ability to render correct SQL, then properly quoted identifiers, then syntax brevity.

Battery of where() tests:

Quality Syntax SQL
->where('col') `col` IS NULL
->where('col', null) `col` IS NULL
->where('col IS NULL') `col` IS NULL
->where(['col' => null]) `col` IS NULL
->where('col IS NULL', null) `col` IS NULL
⚠️ ->where('col', null, false) col IS NULL
⚠️ ->where('col IS NULL', null, false) col IS NULL
⚠️ ->where(['col' => null], null, false) col IS NULL
->where('col', 'NULL', false) col = NULL
->where('col', 'IS NULL', false) col = IS NULL
->where('col', 'NULL') `col` = 'NULL'
->where('col', 'IS NULL') `col` = 'IS NULL'

Battery of set() tests:

Quality Syntax SQL
->set('col', null) `col` = NULL
->set(['col' => null]) `col` = NULL
⚠️ ->set('col', 'NULL', false) col = NULL
->set('col') `col` = ''
->set('col = NULL') `col =` `NULL` = ''
->set('col = NULL', null) `col =` `NULL` = NULL
->set('col = NULL', null, false) col = NULL =
->set('col', '= NULL') `col` = '= NULL'
->set('col', '= NULL', false) col = = NULL
->set('col', 'NULL') `col` = 'NULL'
->set('col', null, false) col =
->set(['col' => null], null, false) col =

⭐ is my recommendation, ✅ means correct and fully quoted, ⚠️ means it will work but not fully quoted, ❌ means it is incorrect

Comments

2

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;');

1 Comment

so i can user the alter table/? it's same with use active record in codeigniter?

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.