CodeIgniter db Operation method

Source: Internet
Author: User
Tags codeigniter

  • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.