0

I am migrating from Codeigniter 3 to Codeigniter 4 and the next query is problematic to me.

In Codeigniter 3, the query was:

$this->db->query("UPDATE myTable SET data = '$data', 
                    myData = IF ( ISNULL(myData), '$state', CONCAT('$state', '-', myData) ) 
                  WHERE id = '$id'");

I tried to change this with the next format:

$data = array(
    'myData' => "IF( ISNULL(myData), " . $state. ", CONCAT(" . $state. ", '-', myData')")
);

However, the function updates the content of $myData like a string "IF( ISNULL(myData), " . stateOfExample. ", CONCAT(" . stateOfExample. ", '-', myData')".

I have looked into the CI4 documentation but I could not find the way to develop this query. I find the way to do that with the where() CI function, but I think is not the best way to do that.

1 Answer 1

1

If you put SQL functions in data, codeigniter won't understand that those are functions. It just adds them as string.

Put IF, CONCAT, ISNULL in sql and use data array just for variables.

$this->db->query("UPDATE myTable SET data = ?, 
    myData = IF ( ISNULL(myData), ?, CONCAT(?, '-', myData) ) 
    WHERE id = ?", array($data, $state, $state, $id));
Sign up to request clarification or add additional context in comments.

3 Comments

This kind of structures works in Codeigniter 3, but as i said i am migrating to Codeigniter 4, that's not follow the Codeigniter queries structure
What do you mean by that? This is from the docs codeigniter.com/user_guide/database/queries.html#query-bindings
You are right, this is a valid query structure in CI4, but it does not work to me

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.