Php 正确的查询方式,RESTAPI Codeigniter
我使用的是chriskacerguis提供的restapi服务器,我认为我的查询方式是错误的,因为每次我单击链接时,即使我只想要id=1,它仍然会显示我数据库中的所有数据 控制器Php 正确的查询方式,RESTAPI Codeigniter,php,codeigniter,api,rest,Php,Codeigniter,Api,Rest,我使用的是chriskacerguis提供的restapi服务器,我认为我的查询方式是错误的,因为每次我单击链接时,即使我只想要id=1,它仍然会显示我数据库中的所有数据 控制器 public function users_get(){ $users = [$this->regusers->get('phar_id')]; $id=$this->get('phar_id'); // If the id parameter doesn't exist re
public function users_get(){
$users = [$this->regusers->get('phar_id')];
$id=$this->get('phar_id');
// If the id parameter doesn't exist return all the users
if ($id === NULL)
{
// Check if the users data store contains users (in case the database result returns NULL)
if ($users)
{
// Set the response and exit
$this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
// Find and return a single record for a particular user.
$id = (int) $id;
// Validate the id.
if ($id <= 0)
{
// Invalid id, set the response and exit.
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// Get the user from the array, using the id as key for retreival.
// Usually a model is to be used for this.
$user = NULL;
if (!empty($users))
{
foreach ($users as $key => $value)
{
if (isset($value['phar_id']) && $value['phar_id'] === $id)
{
$user = $value;
}
}
}
if (!empty($user))
{
$this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
$this->set_response([
'status' => FALSE,
'message' => 'User could not be found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
公共函数用户\u get(){
$users=[$this->reguser->get('phar_id');
$id=$this->get('phar_id');
//如果id参数不存在,则返回所有用户
如果($id==NULL)
{
//检查用户数据存储是否包含用户(如果数据库结果返回NULL)
如果($用户)
{
//设置响应并退出
$this->response($users,REST\u Controller::HTTP\u OK);//OK(200)是HTTP响应代码
}
其他的
{
//设置响应并退出
$this->response([
“状态”=>FALSE,
'消息'=>'未找到任何用户'
],REST_Controller::HTTP_NOT_FOUND);//NOT_FOUND(404)是HTTP响应代码
}
}
//查找并返回特定用户的单个记录。
$id=(int)$id;
//验证id。
if($id response(NULL,REST_Controller::HTTP_BAD_REQUEST);//BAD_REQUEST(400)是HTTP响应代码
}
//使用id作为检索键,从数组中获取用户。
//通常使用一个模型来进行此操作。
$user=NULL;
如果(!空($users))
{
foreach($key=>$value的用户)
{
如果(isset($value['phar\u id'])&&$value['phar\u id']=$id)
{
$user=$value;
}
}
}
如果(!empty($user))
{
$this->set_response($user,REST_Controller::HTTP_OK);//OK(200)是HTTP响应代码
}
其他的
{
$this->set\u响应([
“状态”=>FALSE,
'消息'=>'找不到用户'
],REST_Controller::HTTP_NOT_FOUND);//NOT_FOUND(404)是HTTP响应代码
}
}
模型
<?php
class Regusers Extends CI_Model{
public function get(){
$query = $this->db->get('tblpharmacy');
return $query->result();
}
}
<li><a href="<?php echo site_url('api/example/users'); ?>">Users</a> - defaulting to JSON</li>
<li><a href="<?php echo site_url('api/example/users/format/csv'); ?>">Users</a> - get it in CSV</li>
<li><a href="<?php echo site_url('api/example/users/id/1'); ?>">User #1</a> - defaulting to JSON (users/id/1)</li>
<li><a href="<?php echo site_url('api/example/users/1'); ?>">User #1</a> - defaulting to JSON (users/1)</li>
<li><a href="<?php echo site_url('api/example/users/id/1.xml'); ?>">User #1</a> - get it in XML (users/id/1.xml)</li>
<li><a href="<?php echo site_url('api/example/users/id/1/format/xml'); ?>">User #1</a> - get it in XML (users/id/1/format/xml)</li>
<li><a href="<?php echo site_url('api/example/users/id/1?format=xml'); ?>">User #1</a> - get it in XML (users/id/1?format=xml)</li>
<li><a href="<?php echo site_url('api/example/users/1.xml'); ?>">User #1</a> - get it in XML (users/1.xml)</li>
<li><a id="ajax" href="<?php echo site_url('api/example/users/format/json'); ?>">Users</a> - get it in JSON (AJAX request)</li>
<li><a href="<?php echo site_url('api/example/users.html'); ?>">Users</a> - get it in HTML (users.html)</li>
<li><a href="<?php echo site_url('api/example/users/format/html'); ?>">Users</a> - get it in HTML (users/format/html)</li>
<li><a href="<?php echo site_url('api/example/users?format=html'); ?>">Users</a> - get it in HTML (users?format=html)</li>