PHP:左Join 2表并打印所有记录
我的数据库中有两个表记录,如下所示:PHP:左Join 2表并打印所有记录,php,mysql,Php,Mysql,我的数据库中有两个表记录,如下所示: $tid = FALSE; if(isset($_GET['qid']) && filter_var($_GET['qid'], FILTER_VALIDATE_INT, array('min_range'=>1) ) ){ // create the shorthand of the question ID: $tid = $_GET['tid']; // run query ($q) as shown above $r =
$tid = FALSE;
if(isset($_GET['qid']) && filter_var($_GET['qid'], FILTER_VALIDATE_INT, array('min_range'=>1) ) ){
// create the shorthand of the question ID:
$tid = $_GET['tid'];
// run query ($q) as shown above
$r = mysqli_query($dbc, $q) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $q");
if (!(mysqli_num_rows($r) > 0) ){
$tid = FALSE; // valid topic id
}
}//isset($_GET['qid']
if ($tid) { //OK
$printtopic = FALSE; // flag variable to print topic once
while($content = mysqli_fetch_array($r, MYSQLI_ASSOC)){
if (!$printtopic) {
echo $content['name'];
$printtopic= TRUE;
}
}
} // end of $tid
// Print the messages if any:
echo $content['message'];
WHERE table2.reply_id = {the given topic_id}
表1第1列:
topic_id name
21 my computer
表2各列如下:
reply_id topic_id message
1 21 blabla
2 21 blue
其中,表2中的topic_id列是表1的外键
我想回显表2中的所有回复以及表1中的主题名称(#21)。所以,我提出了这样的问题
$q="SELECT name, message
FROM table1
LEFT JOIN table2
ON table1.topic_id = table2.topic_id
";
但是,结果/输出只返回主题名称和一个回复,而不是预期的2个(或全部)。我错过什么了吗
我使用LEFT JOIN,因为有些主题仍在等待答复。如果没有任何回复,则主题名称仍会在浏览器中打印
我还尝试添加
GROUP BY table1.topic_id
但还是没有运气
你能帮忙吗?谢谢
编辑:为了澄清这个问题,我添加了php代码以获取记录,如下所示:
reply_id topic_id message
1 21 blabla
2 21 blue
如您所知,名称只需打印一次。因此,我的代码如下:
$tid = FALSE;
if(isset($_GET['qid']) && filter_var($_GET['qid'], FILTER_VALIDATE_INT, array('min_range'=>1) ) ){
// create the shorthand of the question ID:
$tid = $_GET['tid'];
// run query ($q) as shown above
$r = mysqli_query($dbc, $q) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $q");
if (!(mysqli_num_rows($r) > 0) ){
$tid = FALSE; // valid topic id
}
}//isset($_GET['qid']
if ($tid) { //OK
$printtopic = FALSE; // flag variable to print topic once
while($content = mysqli_fetch_array($r, MYSQLI_ASSOC)){
if (!$printtopic) {
echo $content['name'];
$printtopic= TRUE;
}
}
} // end of $tid
// Print the messages if any:
echo $content['message'];
WHERE table2.reply_id = {the given topic_id}
$tid=FALSE;
if(isset($获取['qid'])和filter_变量($获取['qid']),filter_VALIDATE_INT,数组('min_range'=>1))){
//创建问题ID的简写:
$tid=$_GET['tid'];
//运行查询($q),如上所示
$r=mysqli_query($dbc,$q)或die(“MySQL错误:”.mysqli_error($dbc)。“
\n查询:$q”);
如果(!(mysqli_num_行($r)>0)){
$tid=FALSE;//有效的主题id
}
}//isset($\u GET['qid']
如果($tid){//OK
$printtopic=FALSE;//将变量标记为打印主题一次
而($content=mysqli\u fetch\u数组($r,mysqli\u ASSOC)){
如果(!$printtopic){
echo$content['name'];
$printtopic=TRUE;
}
}
}//结束$tid
//打印消息(如果有):
echo$content['message'];
试试:
尝试使用内部连接
$q="SELECT name, message
FROM table1
INNER JOIN table2
ON table1.topic_id = table2.topic_id";
在解决这个问题后,我发现问题在于我必须将查询更改为内部联接,并添加WHERE子句,如下所示:
$tid = FALSE;
if(isset($_GET['qid']) && filter_var($_GET['qid'], FILTER_VALIDATE_INT, array('min_range'=>1) ) ){
// create the shorthand of the question ID:
$tid = $_GET['tid'];
// run query ($q) as shown above
$r = mysqli_query($dbc, $q) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $q");
if (!(mysqli_num_rows($r) > 0) ){
$tid = FALSE; // valid topic id
}
}//isset($_GET['qid']
if ($tid) { //OK
$printtopic = FALSE; // flag variable to print topic once
while($content = mysqli_fetch_array($r, MYSQLI_ASSOC)){
if (!$printtopic) {
echo $content['name'];
$printtopic= TRUE;
}
}
} // end of $tid
// Print the messages if any:
echo $content['message'];
WHERE table2.reply_id = {the given topic_id}
那么它工作得很好
很抱歉打扰大家!您能展示一下您的php代码吗?我想问题在于您的php代码如何获取结果?将表2放在左边的第一行根据提供的示例数据集,使用内部或左侧没有关系您访问过吗是的。我想问题出在我的php代码中获取记录。我刚刚添加了php代码中的问题来澄清一下吧。你能帮忙吗?