-1

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

?>
ABC123
  • 13
  • 3
  • So, you mean you want to JOIN: http://dev.mysql.com/doc/refman/5.0/en///join.html – sashkello May 15 '13 at 03:36
  • Maybe? I have tried. Honest. Straight join? left join? When I tried, I couldn't get any data to display in the table. What's the proper format for calling a $row in this instance? [table.var] or just [var]? I'm asking because I got a syntax error with [table.var] and no output with just plain [var]... – ABC123 May 15 '13 at 03:40
  • JOIN vs LEFT JOIN - googlable in 5 seconds. Exactly the same way to access the values by alias (use SELECT something AS "blabla" and then $row['blabla']). Anyway, these are very basics you can learn in 10 minutes browsing through any beginners tutorial. I don't think it's the right place to ask for a guide, especially considering the fact that there are >1 similar questions asked daily... – sashkello May 15 '13 at 03:47
  • if you want all the user then you want a left join, if you want only the users that have posts you want an inner join – Orangepill May 15 '13 at 04:04
  • Thanks for your help. I've googled them both, as well as this very question. Ad nauseum. I have no problem waiting for someone who might be willing to help me figure this out- I didn't seek you out personally and try to waste your time- though I'm sorry that you seem to feel that way. If google could help me, I wouldn't be here. – ABC123 May 15 '13 at 04:07
  • Okay, I agree orangepill, thank you. That's what I've been trying, anyhow. Still can't get the data to display. It has to be a syntax thing, which is why I thought I should ask here, beause I'm not well-versed in mysql, so I don't know HOW to google a syntax problem. – ABC123 May 15 '13 at 04:08
  • $sql = "SELECT phpbb_users.user_id, phpbb_profile_fields_data.user_id FROM `phpbb_users` LEFT JOIN `phpbb_profile_fields_data` on phpbb_users.user_id = phpbb_profile_fields_data.user_id;"; $query = mysql_query( $sql ); echo ""; while( $row = mysql_fetch_assoc($query) ) { echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
    $row[pf_name]$row[pf_business]$row[pf_address]$row[pf_phone]$row[pf_officer]
    "; ?>
    – ABC123 May 15 '13 at 04:10
  • I can't even adequately reply here, maybe it's time I go to bed... ha :-/ (clearly there's a better way to share updated/ different code. I'm happy to do so, if someone would kindly tell me how.) – ABC123 May 15 '13 at 04:11

2 Answers2

1

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>';
Eddie Jaoude
  • 1,698
  • 15
  • 23
  • I *have* to have some sort of syntax error. I still see nothing. Actually, I see less after I try to join tables. :-/ but I do appreciate your input!! – ABC123 May 15 '13 at 04:29
  • If you see less that is fine. You need to do a left join then as some rows do not exist in the 2nd table, i.e. select * from phpbb_users left join phpbb_profile_fields_data on phpbb_profile_fields_data.user_id = phpbb_users.id – Eddie Jaoude May 15 '13 at 04:33
  • i see less than almost exactly nothing.. no a single thing is being echoed. I have tried the left join- it doesn't seem to be collecting any of the data that I want to display though. it has to be in my $row syntax? – ABC123 May 15 '13 at 04:35
  • '$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 ""; while( $row = mysql_fetch_assoc($query) ) { echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
    $row[pf_name]$row[pf_business]$row[pf_address]$row[pf_phone]$row[pf_officer]
    ";'
    – ABC123 May 15 '13 at 04:40
  • On your first while loop or second? Can you post your latest code update? You should only have 1 while loop now containing columns from both original loops. Also can you post both table schemas. i.e. SHOW CREATE TABLE [TABLE NAME] – Eddie Jaoude May 15 '13 at 04:40
  • Is the query correct, because you mentioneed both have 'user_id' columns? Also you keys should be quoted, i.e. $row[user_avatar] should be $row['user_avatar'] – Eddie Jaoude May 15 '13 at 05:00
  • Thanks Eddie- I can't tell you how much I appreciate your help. I'm using double quotes- should I switch to single quotes? I guess I can debug that later... but my question is.... IS the query correct?? I see your question in your code example but I'm not sure if its correct or not! – ABC123 May 15 '13 at 11:53
  • Can you add the schema of both tables, i.e. the columns. Ideally can you run the sql command against both tables (SHOW CREATE TABLE phpbb_users) – Eddie Jaoude May 15 '13 at 12:14
0

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

?>
ABC123
  • 13
  • 3
  • And @Eddie Jaoude you even pointed it out to me. My eyes must have been crossed, because I didn't see it for HOURS. My oh my.... THANK YOU for your patience and help. :) – ABC123 May 15 '13 at 13:12