将2个表与1个公共字段合并 - MySQL

时间:2013-05-15 03:35:25

标签: php mysql phpbb3

我正在尝试在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>";

?>

2 个答案:

答案 0 :(得分:1)

我不知道表结构,但下面是一个例子:

select * from phpbb_users join phpbb_profile_fields_data on phpbb_profile_fields_data.user_id = phpbb_users.id

注意:如果第二个表可能没有匹配的行

,请使用'... left join ...'
$sql = "
SELECT * 
FROM phpbb_users 
LEFT JOIN phpbb_profile_fields_data ON 
     phpbb_profile_fields_data.user_id = phpbb_users.id
";
// QUESTION: should the above be phpbb_users.user_id as you mentioned in comments
$query = mysql_query( $sql );


echo '<table>';

while( $row = mysql_fetch_assoc($query) )
{
     echo '<tr>';
     echo '<td>' . $row['user_avatar'] . '</td>';
     // ...
     echo '<td>' . $row['pf_officer'] . '</td>';
     echo '</tr>';
}

echo '</table>';

答案 1 :(得分:0)

好的 - 我的问题出在我的查询中。我一味地错过了我的“phpbb_profile_fields_data.user_id = phpbb_users.id”,而不是它应该说的,这是“phpbb_profile_fields_data.user_id = phpbb_users。* user _ * id”

Facepalm,我知道......我知道......我非常感谢你的帮助和善意!以下是我的代码的工作副本,如果有人偶然发现并需要它。 :)谢谢!

<?php

$con = mysql_connect( 'hostname', 'username', 'password' );
$db =  mysql_select_db( 'db_name' );

$sql = "select * from phpbb_users left join phpbb_profile_fields_data on phpbb_profile_fields_data.user_id = phpbb_users.user_id";
$query = mysql_query( $sql );

if (!$query) { echo "Error: ".mysql_error(); die(); }

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>";

?>