Inserting data into a table is very easy:
// Select the table
$table = db('mytable');
// Query the database
$table->insert(array(
'user'=>'Evan',
'email'=>'not_real@gmail.com'
));
The above is the same as the following SQL:
INSERT INTO `mytable` (`user`,`email`) VALUES ('Evan','not_real@gmail.com')
Note that the values 'Evan' and 'not_real@gmail.com' are automatically cleaned of any possible SQL Injections.
Get Inserted Row
Insert also returns the new row inserted:
$row = $table->insert(array( 'user'=>'Evan', 'email'=>'not_real@gmail.com' )); // Get the value of the AUTO_INCREMENTING column echo $row->id;
This functionality can be disabled in order to improve peformance by setting the optional second argument to FALSE.
$table->insert(array( 'user'=>'Evan', 'email'=>'not_real@gmail.com' ),FALSE);
© 2008 - 2010 Evan Byrne