2017-09-21 39 views
0

我正在尝试构建一个API以从mySQL返回数据。这是我第一次使用PDO。PDO - 将代码错误传递给函数

我试图传递一个ID到这个下面的函数,以便从tblnames返回所有的名字,其中tblcemetery_ID =无论传递给页面。

我证实,SQL本身是合理的..

任何指针将不胜感激。

结果总是说

{ “消息”: “发现没有人员伤亡。”}

<?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

bindParam功能缺失的参数,因为你没有使用命名的结合。正确的将是$stmt->bindParam(1, $this->id, PDO::PARAM_INT);

链接到文件:http://php.net/manual/en/pdostatement.bindparam.php

+0

遗憾的是它仍然这样做。 –

+0

我知道它使用这条线 '$ row = $ stmt-> fetch(PDO :: FETCH_ASSOC);' –