0
所以这段代码只有在我调用的表匹配请求时才起作用,但是我仍然想要显示主表的所有值,即新闻n表。什么是解决这个在PHP中选择多个表MYSQL
在这里,我刚刚完成我的查询
$query="SELECT * FROM news n,category c, comments a, appusers u, admins w WHERE n.cat_id=c.category_id AND w.userId=n.post_author AND a.post_id=n.id AND u.user_id=a.userID ORDER BY a.commentid DESC, n.id DESC";
$result = mysql_query($query);
$json_response = array();
while($row=mysql_fetch_array($result)) {
if (!isset($json_response[ $row['id'] ])) {
$json_response[ $row['id'] ] = [
'id' => $row['id'],
'title' => $row['title'],
'catId' => $row['cat_id'],
'catName' => $row['category_name'],
'catImage' => $row['category_image'],
'postDate' => $row['post_date'],
'postImage' => $row['post_image'],
'post' => $row['post'],
'commentCount' => $row['comment_count'],
'videoUrl' => $row['video_url'],
'tags' => $row['tags'],
'author' => $row['tags'],
'comments' => [],
];
}
$json_response[ $row['id']]['comments'][] = [
'id' => $row['commentid'],
'comment' => $row['comment'],
'name' => $row['user_name'],
'userId' => $row['userID']
];
}
$data = [];
foreach ($json_response as $element) {
$data[] = $element;
}
echo json_encode($data, JSON_PRETTY_PRINT);
的最佳途径,那么我在这里尝试显示JSON结果
首先和最重要的是:**停止**使用'mysql_'-函数,它们被弃用并且在PHP7.0中,**被移除**。改为使用'mysqli_''或'PDO'。第二:你应该真正统一你的表命名约定。 'category_id','userID','userId'?三种不同的ID命名方案?这必然会引入错误。第三:你真的**是否希望选择**所有**数据,并且每个**语句都运行,没有任何过滤或减少?这会让你的系统在一段时间后变得非常糟糕。 –
谢谢@FranzGleichmann,我一定会留意的。 –