how to access two table are put info together!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

how to access two table are put info together!!!

Post by kedora19 »

got a question that I know parcially of. I know how to access a database and get info but how would i get info from two tables and put the info together.

for example. for twitter it needs to access a database table for what your friends are. then it needs to access the message table to know what to display that your friends wrote

thats the idea i'm getting at, if you still don't understand ask me and i'll try and explain a bit better


thanks in advance, Kedora19
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: how to access two table are put info together!!!

Post by divito »

In general, there are a few ways you could do it. What are you trying to do specifically?
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: how to access two table are put info together!!!

Post by kedora19 »

divito wrote:In general, there are a few ways you could do it. What are you trying to do specifically?
well i haven't really done anything cause i didn't know how to do it. i just know HOW to connect to a database table not how to connect to two at the same time.
mdk999
Forum Newbie
Posts: 22
Joined: Fri May 08, 2009 3:21 pm

Re: how to access two table are put info together!!!

Post by mdk999 »

There are a number of different ways to do this .. the easiest of which is to create as a db.php file which contains two database links say $link1 and $link2. This is then included at the top level of your script. When you use your mysql calls you just add the link you would like to use so ..

Code: Select all

 
$sql = "select * from users";
 
mysql_query($sql,$link1);
 
$sql2 = "select * from old_users";
 
mysql_query($sql2,$link2);
 
 
 
Hope that helps.
Last edited by Benjamin on Fri May 08, 2009 6:01 pm, edited 1 time in total.
Reason: Changed code type from text to php.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: how to access two table are put info together!!!

Post by kedora19 »

mdk999 wrote:There are a number of different ways to do this .. the easiest of which is to create as a db.php file which contains two database links say $link1 and $link2. This is then included at the top level of your script. When you use your mysql calls you just add the link you would like to use so ..

Code: Select all

 
$sql = "select * from users";
 
mysql_query($sql,$link1);
 
$sql2 = "select * from old_users";
 
mysql_query($sql2,$link2);
 
 
 
Hope that helps.
i haven't got a chance to try this out but as soon as i can i will,
thanks
Jody LeCompte
Forum Newbie
Posts: 3
Joined: Sat May 09, 2009 12:01 am

Re: how to access two table are put info together!!!

Post by Jody LeCompte »

That would work if you were accessing two seperate databases, but the problem at hand is selecting from two tables in the same database. In a described scenario, you would probably be loading a list of messages, which would have a user ID, and then load appropriate user information from another table.

What your looking for would be SQL joins. You may have heard some of the vocabulary such as left joins, inner joins, right joins, etc. They allow you to select from two databases and define point(s) of both tables where the information should lock. For instance, users.id and messages.userid.

Some reccomended reading:
http://dev.mysql.com/doc/refman/5.0/en/join.html
Post Reply