$this->db->insert();
Generates
an insert 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(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('tablename', $data);
// Produces: INSERT INTO tablename (title, name, date) VALUES ('My title', 'My name', 'My date')
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('tablename', $data);
// Produces: INSERT INTO tablename (title, name, date) VALUES ('My title', 'My name', 'My date')
The first
parameter will contain the table name, the second is an associative array of
values.
Here is
an example using an object:
/*
class Myclass {
var $title = 'My Title';
var $content = 'My Content';
var $date = 'My Date';
}
*/
$object = new Myclass;
$this->db->insert('tablename', $object);
// Produces: INSERT INTO tablename (title, content, date) VALUES ('My Title', 'My Content', 'My Date')
class Myclass {
var $title = 'My Title';
var $content = 'My Content';
var $date = 'My Date';
}
*/
$object = new Myclass;
$this->db->insert('tablename', $object);
// Produces: INSERT INTO tablename (title, content, date) VALUES ('My Title', 'My Content', 'My Date')
The first
parameter will contain the table name, the second is an object.
Note: All values are escaped
automatically producing safer queries.
$this->db->insert_batch();
Generates
an insert 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' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('tablename', $data);
// Produces: INSERT INTO tablename (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('tablename', $data);
// Produces: INSERT INTO tablename (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
The first
parameter will contain the table name, the second is an associative array of
values.
Note: All values are escaped
automatically producing safer queries.
$this->db->set();
This
function enables you to set values for inserts or updates.
It can be
used instead of passing a data array directly to the insert or update
functions:
$this->db->set('name', $name);
$this->db->insert('tablename');
// Produces: INSERT INTO tablename (name) VALUES ('{$name}')
$this->db->insert('tablename');
// Produces: INSERT INTO tablename (name) VALUES ('{$name}')
If you
use multiple function called they will be assembled properly based on whether
you are doing an insert or an update:
$this->db->set('name', $name);
$this->db->set('title', $title);
$this->db->set('status', $status);
$this->db->insert('tablename');
$this->db->set('title', $title);
$this->db->set('status', $status);
$this->db->insert('tablename');
set() will also accept an optional
third parameter ($escape), that will prevent data from being escaped if set to
FALSE. To illustrate the difference, here is set() used both with and without
the escape parameter.
$this->db->set('field', 'field+1', FALSE);
$this->db->insert('tablename');
// gives INSERT INTO tablename (field) VALUES (field+1)
$this->db->set('field', 'field+1');
$this->db->insert('tablename');
// gives INSERT INTO tablename (field) VALUES ('field+1')
$this->db->insert('tablename');
// gives INSERT INTO tablename (field) VALUES (field+1)
$this->db->set('field', 'field+1');
$this->db->insert('tablename');
// gives INSERT INTO tablename (field) VALUES ('field+1')
You can
also pass an associative array to this function:
$array = array('name' => $name, 'title' => $title, 'status' =>
$status);
$this->db->set($array);
$this->db->insert('tablename');
$this->db->set($array);
$this->db->insert('tablename');
Or an
object:
No comments:
Post a Comment