我正在学习使用不带框架的纯PHP进行的REST API基本创建,读取,删除和更新(我没有REST API的先验知识,这是我第一次尝试进入并学习REST API), GET
请求的一个奇怪问题是在读取数据时请求不返回带有一个属性的结果。
read.php
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/Capteur.php';
// Instantiate DB & connect
$database = new Database();
$db = $database->connect();
// Instantiate capteur object
$capteur = new Capteur($db);
// Capteur read query
$result = $capteur->read();
// Get row count
$num = $result->rowCount();
// Check if any categories
if($num > 0) {
// Cat array
$cap_arr = array();
$cap_arr['data'] = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$cap_item = array(
'id' => $id,
'code_capteur' => $code_capteur,
'etat' => $etat,
'etab' => $etab,
'created_at' => $created_at,
'updated_at' => $updated_at
);
// Push to "data"
array_push($cap_arr['data'], $cap_item);
}
// Turn to JSON & output
echo json_encode($cap_arr);
} else {
// No Categories
echo json_encode(
array('message' => 'No Categories Found')
);
}
当我删除'etab' => $etab
时,它会很好地输出数据
这是我的课程: Capteur.php
<?php
class Capteur {
// DB stuff
private $conn;
private $table = 'capteurs';
// Post Properties
public $id;
public $code_capteur;
public $etat;
public $etab;
public $created_at;
public $updated_at;
// Constructor with DB
public function __construct($db) {
$this->conn = $db;
}
// Get Posts
public function read() {
// Create query
$query = 'SELECT * FROM ' . $this->table . '
ORDER BY
created_at DESC';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Execute query
$stmt->execute();
return $stmt;
}
}
?>