Page 1 of 1

Problem about joining two tables

Posted: Fri Dec 23, 2005 7:28 pm
by Spectrum_tr
Hi again. :) I have a query like this:

Code: Select all

$query = "Select e.id, e.username, e.sdate, e.edate, e.school, e.location
       		  from education e, users u
		  where u.$uname = e.$uname";
where $uname is the variable that holds the username of the user that signed in. (its coming from the session)

but it is not executing the query, i know something is wrong but i can not fix it.

the thing that i want to do is:

the user, that is signed in, is registered in "users" table. I want to find the rows in education table that has the username (the username of the user that logged in). i learned that, combining two tables is like that above. But theres some error about "u.$uname". What will i write instead of it?

Thank you again and again for helping me...

Posted: Fri Dec 23, 2005 7:43 pm
by timvw
Probably something as:

Code: Select all

WHERE e.uname = u.uname AND u.uname = $username
Or rewrite query a little

Code: Select all

SELECT e.id, e.username, e.sdate, e.edate, e.school, e.location
FROM education AS e
INNER JOIN users AS u USING uname
WHERE e.uname = $uname

Posted: Fri Dec 23, 2005 8:12 pm
by Spectrum_tr
Thank you so much... It worked out...

Posted: Fri Dec 23, 2005 8:30 pm
by Spectrum_tr
And i have one second problem...

im using this:

Code: Select all

$query = "insert into education values ( '$id' , '$u_name' , '$s_date' , '$e_date' , '$school' , '$location' )
	  	Select e.id, e.username, e.sdate, e.edate, e.school, e.location
	     	from education e, users u
		where u.username = e.username AND u.username = '$uname'";
for inserting a row into education table where education table has
username = '$uname'

but it does not do it with this code... Why? (Its hard to understand this php language)

Posted: Sat Dec 24, 2005 8:10 am
by timvw
It's hard because you also need to know SQL and not only PHP.

Anyway, you can find an answer to your problem here.

Posted: Sat Dec 24, 2005 8:14 am
by m3mn0n
Moved to the Databases forum.