Problem about joining two tables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Problem about joining two tables

Post 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...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Post by Spectrum_tr »

Thank you so much... It worked out...
Spectrum_tr
Forum Newbie
Posts: 14
Joined: Sat Dec 10, 2005 11:22 am

Post 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)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Moved to the Databases forum.
Post Reply