Page 1 of 1
how to access two table are put info together!!!
Posted: Fri May 08, 2009 5:08 pm
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
Re: how to access two table are put info together!!!
Posted: Fri May 08, 2009 5:19 pm
by divito
In general, there are a few ways you could do it. What are you trying to do specifically?
Re: how to access two table are put info together!!!
Posted: Fri May 08, 2009 5:23 pm
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.
Re: how to access two table are put info together!!!
Posted: Fri May 08, 2009 5:56 pm
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.
Re: how to access two table are put info together!!!
Posted: Fri May 08, 2009 6:32 pm
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
Re: how to access two table are put info together!!!
Posted: Sat May 09, 2009 12:06 am
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