php - 查询数据到json响应
我试图通过PDO从PHP获取数据,然后接收一个JSON对象(json_encode)以在前端使用某些东西。尝试为我可能,我从MySQL查询得到的唯一数据是谁添加的消息(微博)的用户的用户名,见下图: php - 查询数据到json响应
分享Tweet类:从资料Tweet类
class Tweet extends User { private $id;
private $userId;
private $text;
private $creationDate;
public function __construct() {
$this->id = -1;
$this->userId = null;
$this->text = null;
$this->creationDate = null;
}
function getId() {
return $this->id;
}
function getUserId() {
return $this->userId;
}
function getText() {
return $this->text;
}
function getCreationDate() {
return $this->creationDate;
}
function setUserId($userId) {
$this->userId = $userId;
}
function setText($text) {
$this->text = $text;
}
function setCreationDate($creationDate) {
$this->creationDate = $creationDate;
}
static public function loadTweetById(PDO $pdo, $id) {
$stmt = $pdo->prepare("SELECT * FROM Messages WHERE message_id=:id");
$result = $stmt->execute([
'id' => $id
]);
if ($result === true && $stmt->rowCount() > 0) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$loadedTweet = new Tweet();
$loadedTweet->id = $row['message_id'];
$loadedTweet->userId = $row['user_id'];
$loadedTweet->text = $row['message_text'];
$loadedTweet->creationDate = $row['message_datetime'];
return $loadedTweet;
}
return null;
}
static public function loadAllTweetsByUserId(PDO $pdo, $id) {
//$stmt = $pdo->prepare("SELECT * FROM Messages WHERE user_id=:id");
$stmt = $pdo->prepare("SELECT "
. "Users.username, "
. "Messages.message_text, "
. "Messages.message_datetime "
. "FROM "
. "Messages "
. "JOIN "
. "Users "
. "ON Users.id=Messages.user_id "
. "WHERE `user_id`=:id "
. "ORDER BY Messages.message_datetime DESC");
$result = $stmt->execute([
'id' => $id
]);
//var_dump($stmt->rowCount());
if ($result === true && $stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$loadedTweet = new Tweet();
//echo $loadedTweet->id = $row['message_id'] . '<br>';
echo $loadedTweet->userId = $row['username'] . '<br>';
echo $loadedTweet->text = $row['message_text'] . '<br>';
echo $loadedTweet->creationDate = $row['message_datetime'] . '<br>';
echo "<br>";
//return $loadedTweet;
}
return null;
}
}
static public function loadAllTweets(PDO $pdo) {
$sql = "SELECT * FROM Messages JOIN Users ON Users.id=Messages.user_id ORDER BY Messages.message_datetime DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
$tweetsList = [];
if ($stmt !== false && $stmt->rowCount() > 0) {
foreach ($tweets as $dbTweet) {
$tweet = new Tweet($pdo);
$tweet->id = $dbTweet->message_id;
$tweet->userId = $dbTweet->user_id;
$tweet->text = $dbTweet->message_text;
$tweet->creationDate = $dbTweet->message_datetime;
$tweet->getUsername = $dbTweet->username;
$tweetsList[] = $tweet;
}
return $tweetsList;
} else {
return null;
}
}
public function saveToDB(PDO $pdo) {
//sprawdza czy robimy insert czy update
if ($this->id == -1) { // if -1, robimy insert
//przygotowanie zapytania
//$userId = $_SESSION['userId'];
$sql = "INSERT INTO `Messages`(`user_id`, `message_text`) VALUES (:user_id, :message_text)";
$prepare = $pdo->prepare($sql);
//wyslanie zapytania do bazy z kluczami i wartosciami do podmienienia
$result = $prepare->execute([
//uzywa userId zapisanego w sesji
'user_id' => $this->userId,
'message_text' => $this->text
]);
// pobranie ostatniego ID dodanego rekordu i przypisanie do ID w tabeli Users
//$this->id = $pdo->lastInsertId();
return (bool) $result;
}
}
功能loadAllTweets (返回资料Tweet对象的数组):
static public function loadAllTweets(PDO $pdo) { $sql = "SELECT * FROM Messages JOIN Users ON Users.id=Messages.user_id ORDER BY Messages.message_datetime DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
$tweetsList = [];
if ($stmt !== false && $stmt->rowCount() > 0) {
foreach ($tweets as $dbTweet) {
$tweet = new Tweet();
$tweet->id = $dbTweet->message_id;
$tweet->userId = $dbTweet->user_id;
$tweet->text = $dbTweet->message_text;
$tweet->creationDate = $dbTweet->message_datetime;
$tweet->getUsername = $dbTweet->username;
$tweetsList[] = $tweet;
}
return $tweetsList;
} else {
return null;
}
}
mainpage.php
<?php include_once '../../bootstrap.php';
header('Content-Type: application/json');//return json header
$username = $_SESSION['username'];
$tweets = Tweet::loadAllTweets($connection);
$jsonTweets = [];
foreach ($tweets as $tweet) {
$jsonTweets[] = json_decode(json_encode($tweet), true);
}
$response = ["tweets" => $jsonTweets,
"success" => $username];
echo json_encode($response);
AJAX
$.ajax({ url: "../admin/ajax/mainpage.php",
dataType: 'json'
})
.done(function (response) {
console.log(response.tweets);
})
.fail(function (response) {
console.log(response);
});
什么我做错了,非常感谢任何见解。
回答:
Tweet类的私有属性对json_encode
不可见,因此不包含在json中。将它们公开为或实施\JsonSerializable
并使用自定义jsonSerialize实现,例如,
class Tweet implements \JsonSerializable {
// ...
public function jsonSerialize()
{
return [
'foo' => $this->foo,
'bar' => $this->bar
];
}
}
更多的可视性:http://docs.php.net/language.oop5.visibility 不那么明显的事情是,你的静态方法loadTweetData
确实可以访问专用道具,因为对象是同一类的。
注销主题:让您的Tweet模型扩展用户有点尴尬,因为Tweet不是用户(违反Liskov substitution principle)。
以上是 php - 查询数据到json响应 的全部内容, 来源链接: utcz.com/qa/258272.html