$this->load->database();
$insert_hash = array(
'user_name' => '山田 太郎' ,
);
$this->db->insert('my_table', $insert_hash);
$inserted_id = $this->db->insert_id();
$last_sql = $this->db->last_query();
検索条件 : delete_flg = 0
ソート順 : customer_idが小さい順
$this->load->database();
// 設定
$page = 1;
$limit = 20;
$order = 'customer_id ASC';
$hash = array(
'delete_flg' => 0 ,
);
$table_name = 'my_table';
// 件数を取得
$this->db->where($hash);
$total_rows = $this->db->count_all_results($table_name);
// データを取得
$start = $limit * ($page - 1);
$this->db->order_by($order);
$query = $this->db->get_where($table_name, $hash, $limit, $start);
$loop = @$query->result_array();
条件(data_id = 1)
$this->load->database();
$query = $this->db->get_where('my_table', array(
'data_id' => 1 ,
), 1, 0);
$hash = @$query->result_array()[0];
$this->db->where('id', $id);
$this->db->delete('my_table');
例
$this->load->database();
$sql = 'DROP TABLE IF EXISTS my_table';
$query = $this->db->query($sql);
echo $query->result_array();
$array = array('name' => 'hoge');
$this->db->where($array);
$array = array('title' => 'fuga');
$this->db->where($array);
生成されるSQL
WHERE name = 'hoge' AND title = 'fuga'
$array = array('name' => 'hoge');
$this->db->where($array);
$array = array('title' => 'fuga');
$this->db->or_where($array);
生成されるSQL
WHERE name = 'hoge' OR title = 'fuga'
$where = "user_code_name = " . 'BINARY '.$this->db->escape($user_code_name);
$query = $this->db->get_where('my_table', $where, 1, 0);
実行されるSQL
SELECT * FROM `my_table` WHERE user_code_name = BINARY 'H123546789abc' LIMIT 1
$like = array( 'title_name' => 'mail' );
$this->db->like($like);
$like = array( 'content_name' => 'mail' );
$this->db->or_like($like);
生成されるSQL
WHERE "title_name" LIKE '%mail%' ESCAPE '!'
OR "content_name" LIKE '%mail%' ESCAPE '!'
$v = '%%%';
$v = $this->db->escape_like_str($v);
$where = "(title_name like '%{$v}%' OR content_name like '%{$v}%' )";
$this->db->where($where);
生成されるSQL
WHERE ("title_name" like '%!%!%!%%' OR "content_name" like '%!%!%!%%' )
$this->db->like('title', 'match', 'before'); // 次を生成: WHERE `title` LIKE '%match' ESCAPE '!'
$this->db->like('title', 'match', 'after'); // 次を生成: WHERE `title` LIKE 'match%' ESCAPE '!'
$this->db->like('title', 'match', 'both'); // 次を生成: WHERE `title` LIKE '%match%' ESCAPE '!'
$this->db->order_by('title DESC, name ASC');
$this->db->select("item_dt.*, category_dt.category_name as C_category_name");
$this->db->join('category_dt', 'category_dt.category_id = item_dt.category_id', 'left');
$query = $this->db->get_where("item_dt", array('item_dt.data_id' => 123456 ));
$update_hash = array();
$this->db->where('admin_id', $admin_hash['admin_id'] );
$this->db->update('my_table', $update_hash);
$this->db->hostname
$this->db->username
$this->db->password
$this->db->database
$sql = "select count(*) AS COUNT from `information_schema`.`tables` where `table_schema` = '" . $this->db->database . "'";
$query = $this->db->query($sql);
$hash = $query->result_array()[0];
$rows = $this->db->affected_rows()
echo $rows;
$last_sql = $this->db->last_query();
echo $last_sql;