我正在尝试构建一个API来从mySQL返回数据。这是我第一次使用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.")
);
}
?>
答案 0 :(得分:1)
您bindParam
函数缺少参数,因为您没有使用命名绑定。正确的是$stmt->bindParam(1, $this->id, PDO::PARAM_INT);
。