database interaction in codeigniter
CodeIgniter Database Interaction
Load the Database Library: Load the database library using the following code:
php $this->load->database();
Run Queries: You can run queries using the database object. For example:
php $query = $this->db->query('SELECT * FROM my_table');
Using Query Builder: CodeIgniter provides a query builder class to help you create and execute queries. For example:
php $this->db->select('title, content, date'); $this->db->from('my_table'); $this->db->where('id', $id); $query = $this->db->get();
Insert Data: Use the following code to insert data into a table: ```php $data = array( 'title' => 'My title', 'content' => 'My content', 'date' => 'My date' );
$this->db->insert('my_table', $data); ```
- Update Data: Update data using the following code: ```php $data = array( 'title' => $title, 'content' => $content, 'date' => $date );
$this->db->where('id', $id); $this->db->update('my_table', $data); ```
- Delete Data:
Delete data using the following code:
php $this->db->where('id', $id); $this->db->delete('my_table');