Usage collection for DB classes in CodeIgniter

Source: Internet
Author: User
Tags codeigniter
  1. Link Database

  2. -------
  3. $this->load->database ();//manually connect to the database
  4. Connecting multiple databases
  5. $DB 1 = $this->load->database (' Group_one ', TRUE);
  6. $DB 2 = $this->load->database (' Group_two ', TRUE);

  7. Inquire

  8. -------
  9. Parameter binding form
  10. $sql = "SELECT * from some_table WHERE id =? and status =? and author =? ";
  11. $this->db->query ($sql, Array (3, ' live ', ' Rick '));
  12. Multi-Result Standard query
  13. $query = $this->db->query ($sql); Custom
  14. $query = $this->db->get (' tablename '); Convenient form, equivalent to: SELECT * FROM TableName
  15. $query = $this->db->get (' tablename ', 10, 20); Equivalent: SELECT * from TableName LIMIT 20, 10
  16. $query->result ()//Object form
  17. $query->result_array ()//Array form
  18. $query->num_rows ()//total number of bars
  19. $query->num_fields ()//Number of fields
  20. Single-Result standard query
  21. $row = $query->row (); Object Form
  22. $row = $query->row_array (); Array form
  23. Insert
  24. -------
  25. $data = Array (
  26. ' title ' = $title,
  27. ' Name ' = $name
  28. );
  29. $this->db->insert (' tablename ', $data); Easy insertion
  30. $this->db->insert_string (' tablename ', $data); Easy insertion
  31. $this->db->insert_id ()//ID just inserted
  32. $this->db->affected_rows ()//number of rows affected (Update,insert)

  33. Update

  34. -------
  35. $data = Array (
  36. ' Name ' = $name,
  37. ' Email ' = $email
  38. );
  39. $where = "id = 1";
  40. $this->db->update (' tablename ', $data);
  41. $this->db->update_string (' tablename ', $data, $where);

  42. Delete

  43. -------
  44. $array = Array (
  45. ' Name ' = $name,
  46. ' Title ' = $title
  47. );
  48. $this->db->delete (' tablename ', $array);
  49. Produces:
  50. "DELETE from tablename WHERE name = ' $name ' and title = ' $title '"
  51. $this->db->truncate (' tablename '); Clear table
  52. Produce:truncate TableName

  53. (where)

  54. -------
  55. $array = Array (
  56. ' Name ' = $name,
  57. ' Title ' = $title
  58. );
  59. $this->db->where ($array);
  60. Produces: "WHERE name = ' $name ' and title = ' $title '"
  61. -----------------------------------------------------
  62. $this->db->count_all (' tablename '); Total number of rows recorded in table
  63. -----------------------------------------------------
  64. $query->free_result ()//Release resources

Copy Code
  • Related Article

    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.