Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Query Join Two Tables
Old 11-03-2010, 08:46 PM PHP Query Join Two Tables
Average Talker

Posts: 15
Name: Cody
Trades: 0
Hello,

I'm trying to learn PHP / MySQL joins.

Right now I query the database twice to pull out the data i need. Below is my example:

Code:
$result = mysql_query("SELECT * FROM groups WHERE country LIKE '%$city%' ORDER BY rand() LIMIT 5") or die(mysql_error());
	while($r=mysql_fetch_array($result))
	{
        echo $r['groupID'];
			$result2 = mysql_query("SELECT * FROM images WHERE groupID='$r[groupID]' AND home='Y'") or die(mysql_error());
			while($r2 = mysql_fetch_array($result2))
			{
			echo $r2['source'];
                        }
	}
Would someone be able to help me learn MYSQL Joins with PHP / MySQL turning my example of querying the database twice into a joined statement?

Thanks
__________________

Please login or register to view this content. Registration is FREE
Marketplace

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by zytex; 11-03-2010 at 08:48 PM..
zytex is offline
Reply With Quote
View Public Profile Visit zytex's homepage!
 
 
Register now for full access!
Old 11-03-2010, 08:47 PM Re: PHP Query Join Two Tables
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.w3schools.com/sql/sql_join.asp
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-03-2010, 08:56 PM Re: PHP Query Join Two Tables
Average Talker

Posts: 15
Name: Cody
Trades: 0
Thanks for the quick reply,

I'm having some trouble understanding it through words. I just thought I would learn it easier if I saw the above double MySQL query loop as a joined statement.
__________________

Please login or register to view this content. Registration is FREE
Marketplace

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
zytex is offline
Reply With Quote
View Public Profile Visit zytex's homepage!
 
Old 11-03-2010, 11:48 PM Re: PHP Query Join Two Tables
Average Talker

Posts: 15
Name: Cody
Trades: 0
I think I've finally somewhat figured it out, I've been trying to learn this for months no joke.

Thanks for the link,
__________________

Please login or register to view this content. Registration is FREE
Marketplace

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
zytex is offline
Reply With Quote
View Public Profile Visit zytex's homepage!
 
Old 11-04-2010, 12:08 AM Re: PHP Query Join Two Tables
Average Talker

Posts: 22
Trades: 0
Maybe try something like this for your query string:

SELECT * FROM groups as g JOIN images as i on g.groupID = i.groupID WHERE g.country LIKE '%$city%' ORDER BY rand() LIMIT 5

A little test i did on my db was like this:
SELECT *, b.boxname FROM altg_menu as a JOIN altg_menubox as b on a.boxid = b.id WHERE a.id < 20
__________________
The Ultimate Wordpress Themes

Please login or register to view this content. Registration is FREE
swoopster is offline
Reply With Quote
View Public Profile Visit swoopster's homepage!
 
Reply     « Reply to PHP Query Join Two Tables
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26871 seconds with 12 queries