dst2007 2016-04-30 21:22
浏览 78
已采纳

无法在多个表的PHP页面上显示数据

I am building a forum, on my main_forum.php page, I am attempting to display the user who has posted a topic, I get all the data but the username is hosted on another table. How do I get it to display?

<?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');
?>

Here is the table 'forum_questions'

 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 ;

I have successfully connected the table, to the php but there is no username value connected to the table so it doesn't display a user name.

I need to grab the tuple 'first_name' from the table 'users' and apply it to the main_forum.php portion where it denotes which user has created a topic.

Thanks for your help.

  • 写回答

2条回答 默认 最新

  • dtwd74916 2016-04-30 22:42
    关注

    If you add the posers userID to the forum)question table you can then add a new variable which is your users table.

    $table2 = users

    then change your $sql to:

    $sql="SELECT * FROM $tbl_name INNER JOIN $table_name2 ON $tbl_name.user_id = $tbl_name2.user_id ORDER BY id DESC";
    

    now you can access data from both the users table and the forum_question table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊