codeigniter restful web service +数据库查询

时间:2012-06-12 15:30:11

标签: php mysql xml database rest

原谅我即将到来的问题,哈哈。 (我非常陌生)

我已阅读 this tutorial

我已经通过编辑autoload.php文件设置了自动连接到数据库。

我正在创建自己的控制器dsapi.php(下面的代码),我希望ad_get()函数从我的简单数据库中检索所有数据,这样我就可以在我开发的android应用程序中使用它了

我看了 this guidelines page 并尝试写了一些东西,但是我很无知,你可以看到。我甚至接近我想要的东西吗?

class dsapi extends REST_Controller {

    function ad_get()
    {
        **$query = $this->db->query('SELECT index, title, detailed FROM ads');

        foreach ($query->result_array() as $row)
        {
            echo $row['index'];
            echo $row['title'];
            echo $row['detailed'];
        }

        $this->response($query, 200);**
    }

    function ad_put()
    {
        // create a new user and respond with a status/errors
    }

    function ad_post()
    {
        // update an existing user and respond with a status/errors
    }

    function ad_delete()
    {
        // delete a user and respond with a status/errors
    }
}

1 个答案:

答案 0 :(得分:0)

video tutorial description of model 和代码示例

 class Blogmodel extends CI_Model 
 {
 var $title   = '';
 var $content = '';
 var $date    = '';

 function __construct()
 {
     // Call the Model constructor
     parent::__construct();
 }

 function get_last_ten_entries()
 {
     $query = $this->db->get('entries', 10);
     return $query->result();
 }

 function insert_entry()
 {
     $this->title   = $_POST['title']; // please read the below note
     $this->content = $_POST['content'];
     $this->date    = time();

     $this->db->insert('entries', $this);
 }

 function update_entry()
 {
     $this->title   = $_POST['title'];
     $this->content = $_POST['content'];
     $this->date    = time();

     $this->db->update('entries', $this, array('id' => $_POST['id']));
 }}