Collection of usage of DB class in codeigniter

WBOY
Release: 2016-07-25 09:05:36
Original
1148 people have browsed it
  1. Link database

  2. -------
  3. $this->load->database();//Manually connect to the database
  4. //Connect multiple databases
  5. $DB1 = $ this->load->database('group_one', TRUE);
  6. $DB2 = $this->load->database('group_two', TRUE);

  7. Query

  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); //Customized
  14. $query = $this->db->get('tablename'); //Convenient form, equivalent to: SELECT * FROM tablename
  15. $query = $this->db->get('tablename', 10, 20); // Equivalent to: SELECT * FROM tablename LIMIT 20, 10
  16. $query->result() //Object form
  17. $query->result_array() //Array form
  18. $query->num_rows () //Total number of items
  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); //Convenient insertion
  30. $this->db->insert_string('tablename', $data); //Convenient insertion
  31. $ this->db->insert_id() //The id just inserted
  32. $this->db->affected_rows() //The number of affected rows (update,insert)

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

  41. -------
  42. $array = array(
  43. 'name' => $name,
  44. 'title' => $title
  45. );
  46. $this->db->delete( 'tablename', $array);
  47. // Produces:
  48. // "DELETE FROM tablename WHERE name = '$name' AND title = '$title'"
  49. $this->db->truncate('tablename' ); //Clear the table
  50. // Produce: TRUNCATE tablename

  51. (where)

  52. -------
  53. $array = array(
  54. 'name' => $name,
  55. 'title' => $title
  56. );
  57. $this->db->where($array);
  58. // Produces: "WHERE name = '$name' AND title = '$title'"
  59. -------------------------------------------------- ---
  60. $this->db->count_all('tablename'); //The total number of rows recorded in the table
  61. ---------------------- -------------------------------
  62. $query->free_result() //Release resources

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!