Displaying multiple tables seperately


#1

I’m a beginner with php and sql so bare with me.

I have two tables, one that contains user information the second that contains a profile for each user. I have a unique id auto incremented on my user information table and a column for id on my profiles table.

What I am trying to do is when the user access the website they are able to select their name, when they do, it pulls up a page consisted of two tables. The user information (table 1) is displayed at the top of the page, while their profile (table 2) is displayed below it. So I’m not trying to combine the two tables together, just displayed separately on the page. And I want to make sure that it doesn’t show any other information except what corresponds with their assigned id in my user information table and the row(s) that contain their matching id in the profile table.

I’ve read a ton about JOINS and UNIONS but, I’m not sure what my best option would be to go about this. Especially to keep it unique to their page, so that they’re viewing only their information. Any help would be appreciated and I hope I wasn’t confusing!


#2

If you are not combining the data, you do not need to use a join.

Basically, you would do this something like this: (Not code, just general info)

Set up your connection info
Open the connection to the database

set up the query based on the user id to select the profile info
execute the query to load the info you need
display the profile data

set up a second query for the other data based on the user id
execute the query to load the next info you need
display the extra non-profile info

close your database connection

You can do as many queries as needed once the connection is opened. You can take the data an load it into variables or just display it. If you are using PHP to pull this data, you can place into tables (my favorite) or into DIV’s or just HTML. This is fairly easy to do. If you use tables, you would lay out your table with two cols. On the left you would put a title such as “Your User Profile” in the first row and the second row would contain something like “Name: Ernie” or however you want it displayed. When this is pulled from the database it would be something like the results from the query. Perhaps $row. So, displaying the data would be “echo $row[‘username’];” or whatever the fieldname would be.

This is just general ideas to get you started. When you need further help, ask us and we will help!


#3

well, you don’t have to use a join, you can query each table seperately, but if you have a common column (like an id), then using a join would be the way to go. you can use the information anywhere on the page. so assuming the id’s are your common point, the query might look like

[php]
$qry = mysql_query(“SELECT u.id, u.email, u.username, p.name, p.likes
FROM users u
JOIN profiles p (u.id = p.uid)
WHERE u.id = $id)”) or die(mysql_error());

$row = mysql_fetch_assoc($qry);

$name = strtoupper($row[‘username’]);[/php]

IF you don’t have a common column, join won’t work.


#4

Well, he said a User Profile in a table at the top and another one below it, so for him I think two simple separate queries would be easiest for him. Richei is correct about the joins. But, for people who mention not understanding them and using two separate tables, it is much much easier to just use two separate queries and build the tables based on them. I was just thinking of his wording…


#5

Thank you for the information, everyone!

I used your advice, ErnieAlex, and it worked like a sinch! Five minutes of laying out the coding and viola! So thank you! I knew it had to be rather simple and it was :slight_smile:

But thank you Richei for the information on joins, I do want to become more familiar with joins so that information is helpful as well.

Again thank you!