0

我想创建一个 API 来访问我们使用的 MySQL 数据库中有关图书馆顾客和书籍的信息
它基于 CRUD 示例,除了 GET 之外的所有内容都已删除

所以我有 4 个文件:
db_pdo_mysql.php其中包含函数
index.php调用类
book.php包含书籍类
patron.php包含赞助


人类在 index.php 文件中,我将这些类称为:

    require_once '../../restler/restler.php';
    spl_autoload_register('spl_autoload');

    $r = new Restler();
    $r->addAPIClass('book');
    $r->addAPIClass('patron');
    $r->handle();

书.php:

    <?php
   class book {
public $dp;



function __construct(){
    /**
    * $this->dp = new DB_PDO_Sqlite();
    * $this->dp = new DB_PDO_MySQL();
    * $this->dp = new DB_Serialized_File();
    */
    $this->dp = new DB_PDO_MySQL();
}





function get($id=NULL) {
    return is_null($id) ? $this->dp->getAll() : $this->dp->book($id);
}




}
    }
     ?>        


赞助人.php:

    <?php
    class patron {
public $dp;



function __construct(){
    /**
    * $this->dp = new DB_PDO_Sqlite();
    * $this->dp = new DB_PDO_MySQL();
    * $this->dp = new DB_Serialized_File();
    */
    $this->dp = new DB_PDO_MySQL();
}



function get($id=NULL) {
    return is_null($id) ? $this->dp->getAll() : $this->dp->patron($id);
}



    }
    ?>


和 db_pdo_mysql.php

    <?php


    class DB_PDO_MySQL
    {
        private $db;
        function __construct ()
        {
            try {
    //update the dbname username and password to suit your server
        $this->db = new PDO(
        'mysql:host=[HOST REMOVED];dbname=[DATABASE REMOVED]', '[USERNAME REMOVED]',                    '[PASSWORD REMOVED]');
        $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, 
        PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
        throw new RestException(501, 'MySQL: ' . $e->getMessage());
    }
}


function patron ($id)
{
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        $sql = 'SELECT * FROM patrons WHERE code = ' . mysql_escape_string(
        $id);
        return $this->id2int($this->db->query($sql)
            ->fetch());
    } catch (PDOException $e) {

        throw new RestException(501, 'MySQL: ' . $e->getMessage());
    }
}

 function book ($id)
{
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        $sql = 'SELECT * FROM books WHERE barcode = ' . mysql_escape_string(
        $id);
        return $this->id2intBooks($this->db->query($sql)
            ->fetch());
    } catch (PDOException $e) {

        throw new RestException(501, 'MySQL: ' . $e->getMessage());
    }
}
function getAll ()
{
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        $stmt = $this->db->query('SELECT * FROM patrons');
        return $this->id2int($stmt->fetchAll());
    } catch (PDOException $e) {

        throw new RestException(501, 'MySQL: ' . $e->getMessage());
    }
}

private function id2int ($r)
{
    if (is_array($r)) {
        if (isset($r['code'])) {
            $r['code'] = intval($r['code']);

        } else {

            foreach ($r as $a) {
                $a['code'] = intval($a['code']);
            }
        }
    }
    return $r;
}
private function id2intBooks ($r)
{
    if (is_array($r)) {
        if (isset($r['barcode'])) {
            $r['barcode'] = intval($r['barcode']);

        } else {

            foreach ($r as $a) {
                $a['barcode'] = intval($a['barcode']);
            }
        }
    }
    return $r;
}

   }
    ?>


我使用[domain]/api/test/index.php/patron/[patron id][domain]/api/test/index.php/book/[book id]访问数据。
我很困扰!

4

1 回答 1

0

我在 index.php 中犯了一个错误,我没有正确调用赞助人类!

于 2013-10-07T22:42:58.387 回答