FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


JOIN tables, questions





Mgccl
I have read about join but still have many questions in mind.
Here are the list of them
suppose:
table1
id | username
table2
id| password
we join table 1 and table 2 where table1 id = table2 id....
so, the table we will have now(result table) should be
id| username| password
right?

Do we have to SELECT the 2 id fields in both tables or just need them after ON?

THEN how do we access the data in result table?
_________________
AftershockVibe
The fields you SELECT are only the fields you need the query to return. You could even leave them out altogether if you don't need to use the IDs directly otherwise specify one of them.

I'm assuming you're doing an INNER JOIN and don't want to do anything with missing data.

To access the result data using PHP you need to do something like the following:
Code:

<?php

mysql_connect(host,user,pass) or die('Unable To Connect');
// connect to the db

$SQL = 'SELECT table1.id AS "id", table1.username, table2.password FROM table1, table2 INNER JOIN table1 ON table1.id = table2.id' WHERE table1.username = "' . $_POST['user'] . '"';
// Specify your SQL

$query = mysql_query($SQL);
// Run the SQL, $query is the results object

WHILE( $result = mysql_fetch_assoc($query) )
{ // Cycle through each result in the returned object

// Eg: check password matches input

IF($_POST['pass'] != $result['password'])
{
die("invalid password");
}
else
{
// Do some login stuff
}

}


While loop is actually overkill since you should only ever find on unique username but this is a good way to show the shape for general queries.
Related topics
The Unofficial Jokes Thread
CSS OR TABLES
How To : Improve Your PHP Programming
I cant install php chat tables nor set up diffrent subdomain
Site Nav Suggestions...
A Few questions about your service...
Help Needed Regarding CGI And Php
Need Help Yet again :: Comment system, data from 3 tables !!
VERY USEFUL HR QUESTIONS
MySQL JOIN Question
mysql insert into multiple tables at once?
how to create a link on 2 tables in phpmyadmin mysql
Tables to Divs with rowspans
10 Things I've learnt in 2 weeks
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.