- Link Database
- ——-
- $this->load->database ();//manually connect to the database
- Connecting multiple databases
- $DB 1 = $this->load->database (' Group_one ', TRUE);
- $DB 2 = $this->load->database (' Group_two ', TRUE);
- —————————————————–
- Inquire
- ——-
- Parameter binding form
- $sql = "SELECT * from some_table WHERE id =? and status =? and author =? ";
- $this->db->query ($sql, Array (3, ' live ', ' Rick '));
- Multi-Result Standard query
- $query = $this->db->query ($sql); Custom
- $query = $this->db->get (' tablename '); Convenient form, equivalent to: SELECT * FROM TableName
- $query = $this->db->get (' tablename ', 10, 20); Equivalent: SELECT * from TableName LIMIT 20, 10
- $query->result ()//Object form
- $query->result_array ()//Array form
- $query->num_rows ()//total number of bars
- $query->num_fields ()//Number of fields
- Single-Result standard query
- $row = $query->row (); Object Form
- $row = $query->row_array (); Array form
- —————————————————–
- Insert
- ——-
- $data = Array (
- ' title ' = $title,
- ' Name ' = $name
- );
- $this->db->insert (' tablename ', $data); Easy insertion
- $this->db->insert_string (' tablename ', $data); Easy insertion
- $this->db->insert_id ()//ID just inserted
- $this->db->affected_rows ()//number of rows affected (Update,insert)
- —————————————————–
- Update
- ——-
- $data = Array (
- ' Name ' = $name,
- ' Email ' = $email
- );
- $where = "id = 1″;
- $this->db->update (' tablename ', $data);
- $this->db->update_string (' tablename ', $data, $where);
- —————————————————–
- Delete
- ——-
- $array = Array (
- ' Name ' = $name,
- ' Title ' = $title
- );
- $this->db->delete (' tablename ', $array);
- Produces:
- "DELETE from tablename WHERE name = ' $name ' and title = ' $title '"
- $this->db->truncate (' tablename '); Clear table
- Produce:truncate TableName
- —————————————————–
- (where)
- ——-
- $array = Array (
- ' Name ' = $name,
- ' Title ' = $title
- );
- $this->db->where ($array);
- Produces: "WHERE name = ' $name ' and title = ' $title '"
- —————————————————–
- $this->db->count_all (' tablename '); Total number of rows recorded in table
- —————————————————–
- $query->free_result ()//Release resources
CodeIgniter db Operation method