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
how to access two table are put info together!!!
Moderator: General Moderators
Re: how to access two table are put info together!!!
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!!!
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.divito wrote: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!!!
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 ..
Hope that helps.
Code: Select all
$sql = "select * from users";
mysql_query($sql,$link1);
$sql2 = "select * from old_users";
mysql_query($sql2,$link2);
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.
Reason: Changed code type from text to php.
Re: how to access two table are put info together!!!
i haven't got a chance to try this out but as soon as i can i will,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 ..
Hope that helps.Code: Select all
$sql = "select * from users"; mysql_query($sql,$link1); $sql2 = "select * from old_users"; mysql_query($sql2,$link2);
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!!!
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
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