CI batch update $this->db->update_batch ();

Source: Internet
Author: User

$this->db->update_batch ();

Generating an update command is based on the data you provide and executes the query. You can pass an array or an object's arguments to the Update_batch () function. The following is an example of using an array as a parameter: generates an update string based on the data supply, and runs the query. You can either pass a array or an object to the function. A example using an array:

1 $data=Array(2    Array(3' Title ' = ' My title ',4' Name ' = ' My name 2 ',5' Date ' = ' My date 2 '6),7    Array(8' Title ' = ' Another title ',9' Name ' = ' Another Name 2 ',Ten' Date ' = ' Another date 2 ' One    ) A ); -  - $this->db->update_batch (' MyTable ',$data, ' title ');  the  - //produces: - //UPDATE ' mytable ' SET ' name ' = case - //when ' title ' = ' My title ' Then ' My Name 2 ' + //when ' title ' = ' Another title ' Then ' another Name 2 ' - //ELSE ' name ' END, + //' Date ' = case A //when ' title ' = ' My title ' Then ' My date 2 ' at //when ' title ' = ' Another title ' Then ' another date 2 ' - //ELSE ' date ' END - //WHERE ' title ' In (' My title ', ' another title ')

Parameter 1: Table name parameter 2: two-dimensional array parameter 3: Key name as shown above.

Tip: All values will be automatically filtered for security.

That

UPDATE ' MyTable '
SET ' name ' = case
When the ' title ' = ' My title ' Then
' My Name 2 '
When ' title ' = ' Another title ' Then
' Another Name 2 '
ELSE
' name '
END,
' Date ' = case
When the ' title ' = ' My title ' Then
' My date 2 '
When ' title ' = ' Another title ' Then
' Another date 2 '
ELSE
' Date '
END
WHERE
' title ' In (' My title ', ' another title ')

-----------------------------------------------------------

For example, to bulk update status unread read:

$data = Array (
Array
' id ' = ' 1 ',
' Status ' = ' READ '

),
Array
' id ' = ' 2 ',
' Status ' = ' READ '
)
);

$this->db->update_batch (' mytable ', $data, ' id ');

CI batch update $this->db->update_batch ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.