-
-
Link database
- -------
- $this->load->database();//Manually connect to the database
- //Connect multiple databases
- $DB1 = $ this->load->database('group_one', TRUE);
- $DB2 = $this->load->database('group_two', TRUE);
Query
- -------
- //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); //Customized
- $query = $this->db->get('tablename'); //Convenient form, equivalent to: SELECT * FROM tablename
- $query = $this->db->get('tablename', 10, 20); // Equivalent to: SELECT * FROM tablename LIMIT 20, 10
- $query->result() //Object form
- $query->result_array() //Array form
-
- $query->num_rows () //Total number of items
- $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); //Convenient insertion
- $this->db->insert_string('tablename', $data); //Convenient insertion
- $ this->db->insert_id() //The id just inserted
- $this->db->affected_rows() //The number of affected rows (update,insert)
------- - $data = array(
- 'name' => $name,
- 'email' => $email
- );
- $where = "id = 1";
- $ this->db->update('tablename', $data);
- $this->db->update_string('tablename', $data, $where);
------- - $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 the 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'); //The total number of rows recorded in the table
- ---------------------- -------------------------------
- $query->free_result() //Release resources
-
Copy code
|