如何使用 REST API 从表中获取数据

发布于 2024-12-11 01:53:27 字数 1003 浏览 1 评论 0原文

我想使用 Slim 框架在 php 中制作 REST API。

我创建了一个函数来获取表中的所有条目,如下所示:-

<?php

   header('Content-type: application/json');

  // Include the Slim library
  require 'Slim/Slim.php';

  // Instantiate the Slim class
  $app = new Slim();

  // Create a GET-based route
  $app->get('/', function () {
    echo "Pericent is working on Campus Concierge...";
  });

$app->get('/schools', function () 
{ 

    $sql = "SELECT * from school";

    $result = mysql_query($sql) or die ("Query error: " . mysql_error());

    $records = array();
    if (mysql_num_rows($result)==0)
     {

        echo '('.'['.json_encode(array('id' => 0)).']'.')';
     }
     else
     {
       while($row = mysql_fetch_assoc($result)) 
       {

           $records[] = $row;
       }

           echo json_encode($records);
     }

});
?>

现在我想创建一个返回 id 为 5 的学校详细信息的函数。所以请建议我如何创建一个函数,以便我可以访问学校拘留所,网址中给出的 ID 像这样

192.168.1.126/schools/:5

I want to make rest API in php using Slim framework.

I have created a function to get all entries of my table like this:-

<?php

   header('Content-type: application/json');

  // Include the Slim library
  require 'Slim/Slim.php';

  // Instantiate the Slim class
  $app = new Slim();

  // Create a GET-based route
  $app->get('/', function () {
    echo "Pericent is working on Campus Concierge...";
  });

$app->get('/schools', function () 
{ 

    $sql = "SELECT * from school";

    $result = mysql_query($sql) or die ("Query error: " . mysql_error());

    $records = array();
    if (mysql_num_rows($result)==0)
     {

        echo '('.'['.json_encode(array('id' => 0)).']'.')';
     }
     else
     {
       while($row = mysql_fetch_assoc($result)) 
       {

           $records[] = $row;
       }

           echo json_encode($records);
     }

});
?>

Now i want to make a function which returns detail of school which id is 5. so please suggest me how can make a function so i can access school detain which id is given in url like this

192.168.1.126/schools/:5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-12-18 01:53:27

我在您的其他问题上发布了一些类似的信息。我强烈建议您对数据库内容使用对象关系映射。这让我的生活变得超级轻松。听起来您已经了解 SQL,所以您应该能够轻松掌握它。文档很扎实。查看这段代码是否可以满足您的要求。 Idiorm/Paris

class School extends Model {}

$app->get('/school/:id', function($id) use ($app) {

    $school = Model::factory('School')->find_one($id);  // Paris: row with $id
    $schoolAry = $school->as_array('id', 'name', 'zip', 'numStudents');

    $response = $app->response();    // Slim Response object at work
    $response['Content-Type'] = 'application/json';

    echo json_encode($schoolAry);    // Output
});

一旦你有了 GET、POST、PUT、DELETE 路由器,你只需使用Paris 的简单功能,您不必担心长且容易出错的查询字符串。 :-)

I posted some similar information on your other question. I highly recommend using object relational mapping for your DB stuff. It made my life super easy. Sounds like you already know SQL so you should be able to pick it up in a breeze. The documentation is solid. Check out this code that does what you want. Idiorm/Paris

class School extends Model {}

$app->get('/school/:id', function($id) use ($app) {

    $school = Model::factory('School')->find_one($id);  // Paris: row with $id
    $schoolAry = $school->as_array('id', 'name', 'zip', 'numStudents');

    $response = $app->response();    // Slim Response object at work
    $response['Content-Type'] = 'application/json';

    echo json_encode($schoolAry);    // Output
});

Once you have your GET, POST, PUT, DELETE routers in place, you just use the easy features of Paris and you don't have to worry about long error prone query strings. :-)

野侃 2024-12-18 01:53:27

我通过使用一些教程解决了我的问题。

这是我的解决方案:

$app->get('/schools/:id', function ($id) {

// Retrieve game associated with an ID of $id  
$sql = "SELECT * from school where schoolId='$id'";

$result = mysql_query($sql) or die ("Query error: " . mysql_error());

$records = array();
if (mysql_num_rows($result)==0)
 {

  echo '('.'['.json_encode(array('id' => 0)).']'.')';
 }
 else
 {
   while($row = mysql_fetch_assoc($result)) 
   {

     $records[] = $row;
   }

   echo json_encode($records);
 }
});

我希望这对未来的读者有所帮助。

I resolved my problem by using some tutorials.

This is my solution:

$app->get('/schools/:id', function ($id) {

// Retrieve game associated with an ID of $id  
$sql = "SELECT * from school where schoolId='$id'";

$result = mysql_query($sql) or die ("Query error: " . mysql_error());

$records = array();
if (mysql_num_rows($result)==0)
 {

  echo '('.'['.json_encode(array('id' => 0)).']'.')';
 }
 else
 {
   while($row = mysql_fetch_assoc($result)) 
   {

     $records[] = $row;
   }

   echo json_encode($records);
 }
});

I hope this will help future readers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文