从3个表中获取信息SQL&的PHP [英] Getting information from 3 tables SQL & PHP
问题描述
我正在使用HTML,CSS,PHP和MySQl创建类似系统的博客.该站点由三个表组成.
Im making a blog like system using HTML, CSS, PHP and MySQl. The site is made up of three tables.
用户( ID ,用户名,密码,电子邮件)
帖子( postid ,标题,帖子)
来自帖子的评论( postid , id ,评论,commentid)postid和用户的id.
user (id, username, password, email)
posts (postid, title, post)
comments (postid, id, comment, commentid) postid coming from posts and id from user.
我正在尝试显示所有评论以及将其留给特定帖子的用户名.
I am trying to display all of the comments and the users username who left them for a certain post.
当我在phpmyadmin中使用此查询时:从user.id = comments.id上的用户INNER JOIN中选择user.username,comments.comment,其中postid = 1
When i use this query in phpmyadmin: SELECT user.username, comments.comment FROM user INNER JOIN comments on user.id=comments.id where postid=1
它显示了我的需要.
当我将其添加到php中时,我会得到一个空白页.
When i add it into php i get a blank page.
<?php
//echo "1";
session_start();
$connect = mysql_connect('localhost', 'root', 'root') or die("Couldn't connect");
mysql_select_db("com541blog") or die("Couldn't connect to database");
//echo "2";
//$postid = $_GET['type'];
$_SESSION['postid'] = $postid;
//echo "3";
$query_comments = mysql_query("SELECT user.username as username, comments.comment as comment FROM user INNER JOIN comments on user.id=comments.id WHERE postid='1'");
$info = mysql_fetch_array($query_comments);
$username = $info['username'];
$comment = $info['comment'];
echo $username;
echo $comment;
?>
提前感谢您的帮助:)
Thanks in advance for the help :)
推荐答案
我怀疑您的第一行有错误,即在连接"末尾缺少"c".
Your first line has an error I suspect, ie missing 'c' near the end of 'connect'.
include("db_connet.php");
应该是 include("db_connect.php");
此外,缺少分号;
.这个:
Also, missing a semi-colon ;
. This:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1")
应阅读:
$query_comments = ("SELECT user.username, comments.comment
FROM user INNER JOIN comments on user.id=comments.id
where postid=1");
此外,在执行操作时,使用表名来限定每个列名也是不错的做法,例如, user.username
.但是您可能更喜欢使用表别名的以下更简洁的语法:
Also, not bad practice to qualify each of your column names with a table name eg user.username
as you're doing. But you might prefer eg the following more concise syntax using table aliases:
$query_comments = ("SELECT u.username, c.comment
FROM user u INNER JOIN comments c on u.id = c.id
where c.postid = 1");
(请注意,表别名不必是单个字母,因此可以方便地将表名称(例如"ManufacturerSuppliedPartsListData_Feb01")简化为"mpl",而又不会失去其含义.您可以使用客户"和信用",而不只是"c",例如"cust"和"cred")
(Note the table aliases don't need to be a single letter, so can be handy reducing a table name such as "ManufacturerSuppliedPartsListData_Feb01", to eg "mpl", without losing their meaning. Or eg if you've got "Customers" and "Credit" instead of just "c" you might use eg "cust" and "cred")
这篇关于从3个表中获取信息SQL&的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!