2013-10-07 77 views
0

我想創建一個API來訪問MySQL數據庫對圖書館用戶和圖書信息,我們使用
它基於CRUD例子的一切,但得到去除

所以我有4個文件:
db_pdo_mysql.php持有的功能
index.php調用類
book.php保存圖書類
patron.php持有守護神類


在index.php文件中我稱之爲類像這樣:
Restler將無法識別多個類

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

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

book.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); 
} 




} 
    } 
    ?>   


patron.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]訪問數據。
我很困惑!

回答

0

我在index.php中犯了一個錯誤,我沒有正確地調用贊助人類!