-1

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

?>
4

2 回答 2

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>';
于 2013-05-15T04:21:20.400 回答
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>";

?>
于 2013-05-15T13:10:06.727 回答