CodeIgniter (CI) is a good, agile PHP open-source framework, especially the operation of the database, it is very convenient, the following is the PHP CI commonly used database operations, as a record:
/* ================================== Query $query = $this->db_query ("SELECT * from table"); ==================================*///result () returns an array of objects $data = $query->result (); Result_array () returns the data $data = $query->result_array (); Row () returns only one row of an array of objects $data = $query->row (); Num_rows () returns the number of rows of query results $data = $query->num_rows (); Num_fields () returns the number of fields in the query request $data = $query->num_fields (); Row_array () returns only one row of arrays $data = $query->row_array (); Free_result () frees the memory occupied by the current query and removes the associated resource identity $data = $query->free_result (); /* ================================== insert operation ==================================*///The Idecho generated by the last insert Operation $this->db-> INSERT_ID (); Number of rows affected by write and update operations echo $this->db->affected_rows (); Returns the total number of rows in the specified table echo $this->db->count_all (' table_name '); Output the current database version number echo $this->db->version (); Output the current database platform echo $this->db->platform (); Returns the last run of the query statement echo $this->db->last_query (); Insert data, the inserted data will be automatically converted and filtered, for example://$data = Array (' name ' = = $name, ' email ' + $email, ' url ' = = $url); $this->db->insert_string (' table_name ', $data); /* ================================== Update operation ==================================*///Update data, the updated data will be automatically converted and filtered, for example://$data = Array (' name ' = = $name, ' email ' + $email, ' url ' = + $url);//$where = "author_id = 1 and status = ' active '"; $this-& Gt;db->update_string (' table_name ', $data, $where); /* ================================== Select data ==================================*///Get all data of the table $this->db->get (' table_name '); The second parameter is the number of output bars, the third parameter is the start position $this->db->get (' table_name ', 10, 20); Get the data, the first parameter is the table name, the second is the get condition, the third is the number of $this->db->get_where (' table_name ', array (' id ' = = $id), $offset); Select method to get Data $this->db->select (' title, content, Date '); $data = $this->db->get (' table_name '); Gets the maximum value of the field, the second argument is an alias, which is equivalent to Max (age) as Nianling$this->db->select_max (' age '), $this->db->select_max (' age ', ' nianling '); Gets the minimum value of the field $this->db->select_min (' age '); $this->db->select_min (' Age ', ' nianling '); Gets the and $this->db->select_sum of the field (' Age$this->db->select_sum (' Age ', ' nianling '); Custom from Table $this->db->select (' title ', Content, Date '), $this->db->from (' table_name '); Query conditions WHERE name = ' Joe ' and title = ' Boss ' and status = ' active ' $this->db->where (' name ', $name); $this->db->w Here (' title ', $title); $this->db->where (' status ', $status); Range Query $this->db->where_in (' item1 ', ' item2 '); $this->db->where_not_in (' item1 ', ' item2 '); Match, the third parameter matches the pattern title like '%match% ' $this->db->like (' title ', ' Match ', ' Before/after/both '); $this->db-> Not_like (); Group BY title, Date$this->db->group_by (' title ', ' Date '); Limit number of bars $this->db->limit (0, 20);
CodeIgniter common operations on a database