Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Pulling Data From Multiple Tables
Old 11-01-2008, 12:35 PM Pulling Data From Multiple Tables
Novice Talker

Posts: 11
Name: Alex
Trades: 0
Hi everyone,

I have two MySQL tables in one database: 'users' and 'data'

When a user logs in to my website, I want to display their content from the 'data' table on their login page. The problem I'm running into is this: not all users should be able to view all data; instead, users should only be able to view data that belongs to them.

I think I'm over my head. Can anyone give any advice for how to do this?

Thanks!
pico204 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-01-2008, 02:27 PM Re: Pulling Data From Multiple Tables
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Look for a tutorial on databases, you look like you really need one.
Like this one: http://www.sqlcourse.com/

In a relationnal database, what you are looking to do is the base of the system.
You told us that you had 2 tables, users and data.

I'm starting from the principle that a user that is registered in your system have an id.
I'm considering that those users can save datas in your database, and that those datas will have something that will link them to a user.

The step to put everyting together are called "joins". You will tell the database engine that you want to join the inforamtions from the 2 (can be way more than that) tables to display only some that correpond to the filters you will define.

Supposing that your tables look a bit like that:
Code:
users:
  id integer
  username varchar

datas:
  id iteger
  user integer
  data1 varchar
  data2 varchar
In this case, the link between the datas and the users would be the field datas.user
Being an integer, it would mean that the value in that field would match the users.id field, telling that the row X in table datas have been created by the user n° Y

As you already know, you can do 2 select, and parse them in php.
But you can join the tables on that common field too, like this
Code:
select users.username, datas.data1, datas,data2
from users
  inner join datas on datas.user=users.id
But I urgently recommand you to follow the tutorial I've gived you, you relly need to strenghten your sql knowledges if you are to be working with databases.
from
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-01-2008 at 02:28 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-01-2008, 04:22 PM Re: Pulling Data From Multiple Tables
Novice Talker

Posts: 11
Name: Alex
Trades: 0
Wow, thanks for that overview, that was great!

And I'm taking a look at the link you provided now.
pico204 is offline
Reply With Quote
View Public Profile
 
Old 11-02-2008, 09:04 AM Re: Pulling Data From Multiple Tables
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
You should have a read up on sql JOINs. (http://en.wikipedia.org/wiki/JOIN) basically it means associating a row in one table to another row in a different table, based on a join criteria.

For example,
If your table structures were as follows:

User( userID, username, password, name )
Data( dataID, userID, content )

Your query would look something like this

Code:
//Explicit inner join
SELECT * FROM Users u INNER JOIN Data d ON u.userID = d.userID AND u.userID= X

//implicit inner join
SELECT * FROM Users u, Data d WHERE u.userID = d.userID AND u.userID = X
Hope this helps!
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 11-02-2008, 09:05 AM Re: Pulling Data From Multiple Tables
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Sorry, i wrote that last night and forgot to press submit! hope its all going well!
stoot98 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Pulling Data From Multiple Tables
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15064 seconds with 12 queries