我正在尝试在 html 页面中显示来自 MySQL 数据库(由 phpBB3 创建)的用户/配置文件信息。我想在网站上创建一个公共(与 phpBB 无关)页面,该页面将显示所有用户的列表:他们的姓名、地址、电话号码、网站和各种其他个人资料字段。如果所有这些信息都在一张表中,我不会有问题。但是 phpBB 在不同的表中列出了自定义配置文件条目——而且我对 php 或 MySQL 查询不是很方便。我一辈子都无法合并表格。我有大约 50 种不同版本的代码,但没有一个能按照我希望的方式工作。
<?php
$con = mysql_connect( 'hostname', 'username', 'password' );
$db = mysql_select_db( 'dbname' );
//now write a select query to fetch the records from the table
$sql = "select * from phpbb_users";
$query = mysql_query( $sql );
echo "<table border=1>";
//now read and display the entire row of a table one by one looping through it.
//to loop we are using While condition here
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[user_email]</td>";
echo "<td>$row[user_website]</td>";
echo "<td>$row[user_avatar]</td></tr>";
}
echo "</table>";
$sql = "select * from phpbb_profile_fields_data";
$query = mysql_query( $sql );
echo "<table border=1>";
//now read and display the entire row of a table one by one looping through it.
//to loop we are using While condition here
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[pf_name]</td>";
echo "<td>$row[pf_business]</td>";
echo "<td>$row[pf_address]</td>";
echo "<td>$row[pf_phone]</td>";
echo "<td>$row[pf_officer]</td></tr>";
}
echo "</table>";
?>
这将显示两个单独的表格,其中包含我想要包含的所有数据。我只希望这些表显示为一个。第二个表包含我想首先显示的信息,但与第一个表中的其余数据内联。我知道这是一个愚蠢的问题。对不起。在我尝试实现之前,这听起来很简单。谢谢你的帮助!:)
更新代码:
<?php
$con = mysql_connect( 'hostname', 'username', 'password' );
$db = mysql_select_db( 'dbname' );
$sql = "select * from phpbb_users left join phpbb_profile_fields_data on phpbb_profile_fields_data.user_id = phpbb_users.id";
$query = mysql_query( $sql );
echo "<table>";
while( $row = mysql_fetch_assoc($query) )
{
echo "<tr><td>$row[user_avatar]</td>";
echo "<td>$row[pf_name]</td>";
echo "<td>$row[pf_business]</td>";
echo "<td>$row[pf_address]</td>";
echo "<td>$row[pf_phone]</td>";
echo "<td>$row[user_email]</td>";
echo "<td>$row[user_website]</td>";
echo "<td>$row[pf_officer]</td></tr>";
}
echo "</table>";
?>