php api ci,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());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值