1.全表查詢
public function query()
{
$sql = "查詢語句";
$query = $this->db->query($sql);
$this->output->set_output(json_encode($query->result()));
}
2.帶引數查詢(get請求)
public function queryByParam($param)
{
$sql = "查詢語句 where 欄位={$param}";
$query = $this->db->query($sql);
$this->output->set_output(json_encode($query->result()));
}
3.帶引數查詢(post請求)
public function detail()
{
$jsonStr = $this->input->raw_input_stream;
$jsonObj = json_decode($jsonStr);
$param = $jsonObj->id;
$sql = "查詢語句 from 表名 where 欄位 = `{$param}` ";
$query = $this->db->query($sql);
$resData = $query->result();
$this->output->set_output(json_encode($resData));
}
4.分頁查詢
public function paginate()
{
$jsonStr = $this->input->raw_input_stream;
$jsonObj = json_decode($jsonStr);
$page = $jsonObj->page; //$page=1
$limit = $jsonObj->limit; //$limit=2
if($page < 1 )
{
$page = 1;
}
$tmp = ($page-1)*$limit;
$sql = "查詢語句 from 表名 limit {$tmp},{$limit}";
$query = $this->db->query($sql);
$this->output->set_output(json_encode($query->result()));
}
5.儲存
public function save()
{
$jsonStr = $this->input->raw_input_stream;
$jsonObj = json_decode($jsonStr);
$params = array(
`id` => $jsonObj->id,
`name` => $jsonObj->title,
`content` => $jsonObj->content
);
$this->db->insert(`表名`,$params);
$this->output->set_output($this->db->affected_rows());
}