1 Comment

Active Record Pattern SELECT and INSERT Active record in CodeIgniter

Standard Insert

$sql = "INSERT INTO mytable (title, name) 
        VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";
$this->db->query($sql);
echo $this->db->affected_rows();

Active Record Query

The Active Record Pattern gives you a simplified means of retrieving data:

$query = $this->db->get('table_name');
foreach ($query->result() as $row)
{
    echo $row->title;
}

The above get() function retrieves all the results from the supplied table. The Active Record class contains a full compliment of functions for working with data.

Active Record Insert

$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')

One comment on “Active Record Pattern SELECT and INSERT Active record in CodeIgniter

Leave a comment