codeigniter db操作方法

來源:互聯網
上載者:User

標籤:

  • 連結資料庫
  • ——-
  • $this->load->database();//手動串連資料庫
  • //串連多資料庫
  • $DB1 = $this->load->database(‘group_one’, TRUE);
  • $DB2 = $this->load->database(‘group_two’, TRUE);
  • —————————————————–
  • 查詢
  • ——-
  • //參數綁定形式
  • $sql = “SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?”;
  • $this->db->query($sql, array(3, ‘live’, ‘Rick’));
  • //多結果標準查詢
  • $query = $this->db->query($sql); //自訂
  • $query = $this->db->get(‘tablename’); //便捷形式,相當於:SELECT * FROM tablename
  • $query = $this->db->get(‘tablename’, 10, 20); // 相當於: SELECT * FROM tablename LIMIT 20, 10
  • $query->result() //對象形式
  • $query->result_array() //數組形式
  • $query->num_rows() //總條數
  • $query->num_fields() //欄位數
  • //單結果標準查詢
  • $row = $query->row(); //對象形式
  • $row = $query->row_array(); //數組形式
  • —————————————————–
  • 插入
  • ——-
  • $data = array(
  • ‘title’ => $title,
  • ‘name’ => $name
  • );
  • $this->db->insert(‘tablename’, $data); //便捷插入
  • $this->db->insert_string(‘tablename’, $data);  //便捷插入
  • $this->db->insert_id() //剛插入的id
  • $this->db->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’); //清空表
  • // 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’); //表中記錄總行數
  • —————————————————–
  • $query->free_result() //釋放資源

codeigniter db操作方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.