首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从多个表在PHP页面上显示数据

问无法从多个表在PHP页面上显示数据
EN

Stack Overflow用户
提问于 2016-04-30 21:22:29
回答 2查看 96关注 0票数 0

我正在构建一个论坛,在我的main_forum.php页面上,我试图显示发布了一个主题的用户,我得到了所有的数据,但是用户名是托管在另一个表上的。我怎样才能把它展示出来?

代码语言:javascript
复制
<?php

include ('includes/session.php');
include ('includes/header.php');

$host = "localhost";
$username = "fses16g6";
$password = "fses16g6";
$db_name="fses16g6"; // Database name 
$tbl_name="forum_question"; // Table name 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
//$query="SELECT * FROM users"
// OREDER BY id DESC is order result by descending 

$result=mysql_query($sql);
?>

<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1"     bgcolor="#202531">
<tr>
<td width="6%" align="center" bgcolor="#202531"><strong>#</strong></td>
<td width="50%" align="center" bgcolor="#202531"><strong>Topic</strong></td>
<td width="11%" align="center" bgcolor="#202531"><strong>User</strong></td>
<td width="11%" align="center" bgcolor="#202531"><strong>Views</strong></td>
<td width="11%" align="center" bgcolor="#202531"><strong>Replies</strong>    </td>
<td width="11%" align="center" bgcolor="#202531"><strong>Date/Time</strong>    </td>
</tr>

<?php

while($rows=mysql_fetch_array($result)){
?>

<tr>
<td bgcolor="#202531"><?php echo $rows['id']; ?></td>
<td bgcolor="#202531"><a href="view_topic.php?id=<?php echo $rows['id']; ?    >"><?php echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#202531"><?php echo $rows['first_name']; ?></td>
<td align="center" bgcolor="#202531"><?php echo $rows['view']; ?></td>
<td align="center" bgcolor="#202531"><?php echo $rows['reply']; ?></td>
<td align="center" bgcolor="#202531"><?php echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection 
}

mysql_close();
?>

<tr>
<td colspan="7" align="right" bgcolor="#000000"><a href="create_topic.php">    <strong>Create New Topic</strong> </a></td>
</tr>
</table>



<?php

if ($is_admin) {
                echo '<button type="button">EDIT</button>';
                echo '<button type="button">DELETE</button>';
            }

include ('includes/footer.html');
?>

这是'forum_questions‘表

代码语言:javascript
复制
 CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default '',
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default '',
`email` varchar(65) NOT NULL default '',
`datetime` varchar(25) NOT NULL default '',
`view` int(4) NOT NULL default '0',
`reply` int(4) NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

我已经成功地将表连接到php,但是没有用户名值连接到表中,所以它不显示用户名。

我需要从表‘user’中获取元组'first_name‘,并将其应用到main_forum.php部分,其中表示哪个用户创建了一个主题。

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-30 22:42:08

如果将偏执狂userID添加到论坛)问题表中,则可以添加一个新变量,即用户表。

$table2 = users

然后将$sql更改为:

代码语言:javascript
复制
$sql="SELECT * FROM $tbl_name INNER JOIN $table_name2 ON $tbl_name.user_id = $tbl_name2.user_id ORDER BY id DESC";

现在,您可以访问users表和forum_question表中的数据。

票数 0
EN

Stack Overflow用户

发布于 2016-04-30 21:37:39

我认为您问题的精神与SQL有关,而与PHP无关。您可以考虑查看这个问题,它显示为与您的问题相关。这将使您开始使用所需的SQL语法。

从表设计的角度来看,您需要确保有一些方法可以将包含用户数据的表与包含论坛问题数据的表关联起来。作为关系数据库,拥有MySQL数据库的强大功能是将数据关联在一起,可能来自不同的表。一个快速的谷歌搜索发现了本底漆的MySQL数据库设计,但有大量的资源,您可以通过一些快速搜索。

简而言之,您可能想探索创建一个将您的用户与您的论坛问题相关联的外键。然后,可以使用适当的SQL语法获取包含用户信息的查询结果。

我希望这能帮助你走上正确的道路!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36960387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档