データベースで1つのレコードを読み取るために、残りのAPIを作成していましたが、出力からNULL値が返されました。 IDが与えられたときに製品の詳細を与えるべきです。このRest APIのデータベースとしてphpmyadminを使用します。残りのレコードは1つのレコードを読み取るためにnull値を返します
read_one.php
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Credentials: true");
header('Content-Type: application/json');
// include database and object files
include_once '../config/database.php';
include_once '../objects/product.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
// prepare product object
$product = new Product($db);
// set ID property of product to be edited
$product->id = isset($_GET['id']) ? $_GET['id'] : die();
// read the details of product to be edited
$product->readOne();
// create array
$product_arr = array(
"id" => $product->id,
"category_id" => $product->category_id,
"title" => $product->title,
"model" => $product->model,
"brand" => $product->brand,
"colour_flavour" => $product->colour_flavour,
"price_exclude_gst" => $product->price_exclude_gst,
"gst" => $product->gst,
"buy" => $product->buy,
"price" => $product->price,
"comm" => $product->comm,
"type_id" => $product->type_id
);
// make it json format
print_r(json_encode($product_arr));
?>
機能readOne
function readOne(){
// query to read single record
$query = "SELECT
c.title as title, p.id, p.category_id, p.title, p.model,
p.colour_flavour, p.brand, p.price_exclude_gst, p.gst,
p.buy, p.price, p.comm, p.type_id
FROM
" . $this->table_name . " p
LEFT JOIN
categories c
ON p.id = c.category_id
WHERE
p.id = ?
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->id = $row['id'];
$this->category_id = $row['price'];
$this->title = $row['title'];
$this->model = $row['model'];
$this->colour_flavour = $row['colour_flavour'];
$this->brand = $row['brand'];
$this->price_exclude_gst = $row['price_exclude_gst'];
$this->gst = $row['gst'];
$this->buy = $row['buy'];
$this->price = $row['price'];
$this->comm = $row['comm'];
$this->type_id = $row['type_id'];
}