CodeIgniter基本接口api

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());
}

你可能感兴趣的:(codeigniter框架,php)