2017-09-19 50 views
0

我正在嘗試構建一個API以從mySQL返回數據。這是我第一次使用PDO。當我嘗試匹配對象時映射PDO列不工作

如果我做下面的,只有ID被映射而已。我第一次使用這個,所以我不知道在哪裏看。另外,我從來沒有能夠讓它在本地運行,所以它運行在Live站點上,所以我想不出一種方法來調試它。

查詢結果看起來像這樣

{ 
ID: "18", 
Name: null, 
Country: null 
} 
class cemetery{ 

    // database connection and table name 
    private $conn; 

    // object properties 
    public $id; 
    public $fldcemetery; 
    public $fldcountry;  

    public function __construct($db){ 
     $this->conn = $db; 
    } 

    public function readOne(){ 
     // query to read single record 
       $query = "select tblcemetery.id, tblcemetery.tblcountry_ID, tblcemetery.fldcemetery, tblcountry.fldcountry from tblcemetery 
       left join tblcountry on 
       tblcemetery.tblcountry_ID = tblcountry.ID 
       WHERE 
       id = ? 
       ORDER BY 
         fldcemetery 
       LIMIT 0,1"; 

     // prepare query statement 
     $stmt = $this->conn->prepare($query); 

     // bind id of product to be updated 
     $stmt->bindParam(1, $this->id); 

     // execute query 
     $stmt->execute(); 

     // get retrieved row 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 

     // set values to object properties 
     $this->fldcemetery = $row['fldcemetery']; 
     $this->fldcountry = $row['fldcountry'];  

    } 

} 
// get database connection 
$database = new Database(); 
$db = $database->getConnection(); 

// prepare cemetery object 
$cemetery = new cemetery($db); 

// set ID property of record to read 
$cemetery->id = isset($_GET['id']) ? $_GET['id'] : die(); 

// read the details of cemetery to be edited 
$cemetery->readOne(); 

// create array 
$cemetery_arr = array(
    "ID" => $cemetery->id, 
    "Name" => $cemetery->fldcemetery, 
    "Country" => $cemetery->fldcountry 
); 

// make it json format 
print_r(json_encode($cemetery_arr)); 
+1

如果單獨運行查詢(換出?像18的ID),你得到的行回來? –

+0

是,返回相同的數據'{ ID:「18」, 名稱:空, 國家:空 }' –

+1

只是爲了澄清,我說的是在MySQL工作臺,phpMyAdmin的等 –

回答

0
WHERE id = ? 

應該已經

WHERE tblcemetery.id = ? 
0

我認爲對於那些價值的關鍵必須是$row['tblcemetery.fldcemetery']

使用在你的SQL查詢來更改密鑰中結果集。

相關問題