[SOLVED] MySQL query problem - Looped results

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
sandstorm140
Forum Newbie
Posts: 10
Joined: Mon Mar 02, 2009 6:17 am

[SOLVED] MySQL query problem - Looped results

Post by sandstorm140 »

Hello,
I'm using the following code. I'm wanting to display usernames from
database: usernames
table: users
from field: online
where online = 1.

As you can see, i also have FROM accounts.accounts because later on I will need to pull information from that database simultaneously.
Database: accounts
table: accounts
--Also has a field named "online".

My problem is I should have 4 results equal to usernames.users.online=1.
I get the correct 4 names, however, it seems to be looped over and over again hundreds of times.

For example:
username1
username2
username3
username4
//starts again..
username1
username2
username3
username4
...etc

Code: Select all

 
SELECT
usernames.users.name, usernames.users.online
FROM
usernames.users,
accounts.accounts
WHERE
usernames.users.online =  '1'
 
I'm fairly new to MySQL, any help/advice would be splendid.

Thanks in advance.
Last edited by sandstorm140 on Wed Mar 04, 2009 6:16 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: MySQL query problem - Looped results

Post by VladSun »

Use a properly build JOIN - http://www.w3schools.com/Sql/sql_join.asp
With your query you perform a CROSS JOIN and you need a INNER JOIN

PS: How many databases you have?!?
There are 10 types of people in this world, those who understand binary and those who don't
sandstorm140
Forum Newbie
Posts: 10
Joined: Mon Mar 02, 2009 6:17 am

Re: MySQL query problem - Looped results

Post by sandstorm140 »

Thanks I'll read up on that.

I have just 3 databases. It's really nothing important, just made them for educational purposes.
sandstorm140
Forum Newbie
Posts: 10
Joined: Mon Mar 02, 2009 6:17 am

Re: MySQL query problem - Looped results

Post by sandstorm140 »

Thanks It worked
Post Reply