Connecting Tech Pros Worldwide Forums | Help | Site Map

Retrieving data from multiple tables and making multiple pages?

chelf's Avatar
Member
 
Join Date: Jan 2007
Posts: 54
#1: Jan 6 '09
Hello again,

First I want to say thank you to all of you that have helped me in the past with all of my questions. I have gotten far in PHP and MySQL in the past two weeks. I designed a site that pulls information from a database and displays it on the website. However I have run into some questions:

1. I have a database with multiple tables. One table consists of a user profile type of some sort, the other table consists of a Bio of that user. Now I want to link the profile with the bio so I can display information from both tables. How can I do that. I understand the following code:

Expand|Select|Wrap|Line Numbers
  1. $results = my_sql_query("SELECT * from profile WHERE user = 'username'");
  2. while ($row = mysql_fetch_array($results))
  3. {
  4. echo "user: " . $row['username']
  5. }
  6.  
How can I get information from the second table to display? They both have a common field called username that match.

Also, can anyone show me how to display results in multiple result pages?

Thanks!
debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,507
#2: Jan 6 '09

re: Retrieving data from multiple tables and making multiple pages?


You simply need to join both the tables to retrive data.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#3: Jan 6 '09

re: Retrieving data from multiple tables and making multiple pages?


Hi.

You can JOIN them together, essentially creating one big table out of them both.

Like, if you had these two tables:
Expand|Select|Wrap|Line Numbers
  1. /* User */
  2. +--------+----------+
  3. | UserID | UserName |
  4. +--------+----------+
  5. |      1 | First    |
  6. |      2 | Second   |
  7. +--------+----------+
  8. /* UserInfo */
  9. +-----------+-----------+----------+
  10. | UserID_FK | FirstName | LastName |
  11. +-----------+-----------+----------+
  12. |         1 | First     | User     |
  13. |         2 | Second    | User     |
  14. +-----------+-----------+----------+
To get all the info (ID, username, real name) from a single query, you can JOIN them using the UserID.

Like:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   u.UserID,
  3.   u.UserName,
  4.   i.FirstName,
  5.   i.LastName
  6. FROM User AS u
  7. INNER JOIN UserInfo AS i
  8.   ON u.UserID = i.UserID_FK
Which would give you:
Expand|Select|Wrap|Line Numbers
  1. +--------+----------+-----------+----------+
  2. | UserID | UserName | FirstName | LastName |
  3. +--------+----------+-----------+----------+
  4. |      1 | First    | First     | User     |
  5. |      2 | Second   | Second    | User     |
  6. +--------+----------+-----------+----------+
I used an INNER JOIN there, but that is not always the best type of join to use.
See the differences between the different joins here.
chelf's Avatar
Member
 
Join Date: Jan 2007
Posts: 54
#4: Jan 7 '09

re: Retrieving data from multiple tables and making multiple pages?


Thank you again!! I used the LEFT JOIN statement and was able to work things out. Can you show me a sample code on how to break search results into multiple pages?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#5: Jan 7 '09

re: Retrieving data from multiple tables and making multiple pages?


That would be pagination.
There is a lot of material on that topic on the www.
Try a Google search using pagination as they keyword.

This tutorial looks pretty good.
Reply


Similar MySQL Database bytes