سؤال

I'm trying to display user/profile information from a MySQL database (created by phpBB3) in an html page. I want to create a public (not related to phpBB) page on a website that will display a list of all users: their names, addresses, phone numbers, websites, and various other profile fields. If all of this information was in one table, I wouldn't have a problem. But phpBB lists custom profile entries in a different table- and I'm not very handy with php or MySQL queries. I can't for the life of me get the tables to merge. I have about 50 different versions of this code, but none of them work the way I want them to.

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

?>

This displays two separate tables with all of the data that I want to include. I just want these tables to display as one. The second table has the info that I want to display first- but inline with the rest of the data from the first table. I know this is a dumb question. I'm sorry. This sounded SO simple before I tried to make it happen. Thanks for your help! :)

Updated Code:

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

?>
هل كانت مفيدة؟

المحلول

I dont know the table structure, but below is an example:

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

NB: Use '...left join...' if the 2nd table might not have a matching row

$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>';

نصائح أخرى

Okay- my problem was with my query. I blindly missed that I had "phpbb_profile_fields_data.user_id = phpbb_users.id" instead of what it should have said, which is "phpbb_profile_fields_data.user_id = phpbb_users.*user_*id"

Facepalm, I know.... I know.... I really do appreciate your help and kindness! The following is a working copy of my code, should anyone ever stumble upon this and need it. :) THANK YOU!

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

?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top