2017-09-21 8 views
0

mySQLからデータを返すAPIを構築しようとしています。これはPDOを使用した私の最初の時の1つです。PDO - 機能にIDを渡すコードエラー

tblcemetery_ID =何かがページに渡されたときに、tblnamesからすべての名前を返すために、この下の関数にIDを渡そうとしています。任意のポインタをいただければ幸いです

私はSQL自体が健全であることを確認した

..

結果常に

{ "メッセージ": "死傷者が見つかりませんでした。"}と言うあなたはbindParam関数は、パラメータが欠落してい

<?php 
class casualty{ 

    // database connection and table name 
    private $conn; 
    private $table_name = "tblcasualty"; 

    // object properties 
    public $id; 
    public $fldgraphicname; 


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

    // used by select drop-down list 
    public function cemetery(){    
     // query to read single record 
        $query = "SELECT * 
         FROM 
         `mapleleaf`.`tblnames` 
          WHERE 
         `tblcemetery_ID` = ? 
          ORDER BY ID 
         "; 

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

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

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

      return $stmt; 
    }   

} 
?> 
<?php 
// required header 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 

// include database and object files 
include_once '../config/database.php'; 
include_once '../objects/casualty.php'; 

// instantiate database and casualty object 
$database = new Database(); 
$db = $database->getConnection(); 

// initialize object 
$casualty = new casualty($db); 

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

// query categorys 
$stmt = $casualty->cemetery(); 
$num = $stmt->rowCount(); 

// check if more than 0 record found 
if($num>0){ 

    // products array 
    $casualtys_arr=array(); 
    $casualtys_arr["records"]=array(); 

    // retrieve our table contents 
    // fetch() is faster than fetchAll() 
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop 
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ 
     // extract row 
     // this will make $row['name'] to 
     // just $name only 
     extract($row); 

     $casualty_item=array(
      "ID" => $ID, 
      "filename" => $fldgraphicname      
     ); 

     array_push($casualtys_arr["records"], $casualty_item); 
    } 

    echo json_encode($casualtys_arr); 
} 

else{ 
    echo json_encode(
     array("message" => "No Casualties found.") 
    ); 
} 
?> 

答えて

1

、名前付きバインドを使用していないので、 。正しいのは$stmt->bindParam(1, $this->id, PDO::PARAM_INT);です。ドキュメントへ

リンク:http://php.net/manual/en/pdostatement.bindparam.php

+0

は、残念ながらそれはまだ同じことをやっています。 –

+0

この行を使って作業しています '$ row = $ stmt-> fetch(PDO :: FETCH_ASSOC);' –