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.")
);
}
?>
遺憾的是它仍然這樣做。 –
我知道它使用這條線 '$ row = $ stmt-> fetch(PDO :: FETCH_ASSOC);' –