$this->db->update();
Generates
an update string and runs the query based on the data you supply. You can pass
an array or an object to the function. Here is an example using
an array:
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('tablename', $data);
// Produces:
// UPDATE tablename
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('tablename', $data);
// Produces:
// UPDATE tablename
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
Or you
can supply an object:
/*
class Myclass {
var $title = 'My Title';
var $content = 'My Content';
var $date = 'My Date';
}
*/
$object = new Myclass;
$this->db->where('id', $id);
$this->db->update('tablename', $object);
// Produces:
// UPDATE tablename
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
class Myclass {
var $title = 'My Title';
var $content = 'My Content';
var $date = 'My Date';
}
*/
$object = new Myclass;
$this->db->where('id', $id);
$this->db->update('tablename', $object);
// Produces:
// UPDATE tablename
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
Note: All values are escaped
automatically producing safer queries.
You'll
notice the use of the $this->db->where() function, enabling you to
set the WHERE clause. You can optionally pass this information directly into
the update function as a string:
$this->db->update('tablename', $data, "id = 4");
Or as an
array:
$this->db->update('tablename', $data, array('id' => $id));
You may
also use the $this->db->set() function described above when
performing updates.
$this->db->update_batch();
Generates
an update string based on the data you supply, and runs the query. You can
either pass an array or an object to the function. Here is an
example using an array:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('tablename', $data, 'title');
// Produces:
// UPDATE `tablename` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE
// WHEN `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')
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('tablename', $data, 'title');
// Produces:
// UPDATE `tablename` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE
// WHEN `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')
The first
parameter will contain the table name, the second is an associative array of
values, the third parameter is the where key.
No comments:
Post a Comment