DB Outputs twice, Why?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

DB Outputs twice, Why?

Post by Goofan »

Hi,

My Current code outputs the same ID twice.
The rid column in my db contains 1,2,3,4,5 But my code outputs_
Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 2 [4] => 3 [5] => 3 [6] => 4 [7] => 4 [8] => 5 [9] => 5 )
I don't know the reason for this and am therefor asking you guys for help.

CODE:

Code: Select all

$mydb = "SELECT * FROM research, economy";
        $res = $db->dbquery($mydb);
        while($array = $db->fetchArray($res)){
					
	
	$ptf = $array['pointstofinish'];
	$rid[] = $array['rid'];
	$Research[] = $array['name'];
}
Regards,
Thomas
User avatar
Tiancris
Forum Commoner
Posts: 39
Joined: Sun Jan 08, 2012 9:54 pm
Location: Mar del Plata, Argentina

Re: DB Outputs twice, Why?

Post by Tiancris »

I guess both tables have a field named "rid", so you are loading $rid[] with "research.rid" and "economy.rid".
Instead of "SELECT *" you should specify the fields in each table, and probably use aliases in some of them. And perhaps you are missing a JOIN between tables too.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: DB Outputs twice, Why?

Post by Goofan »

It was the Join statement.

I need to look into it. As i seperated the tables in the request it works.

I'll look into JOIN statement from now :)

Regards,
Thomas
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: DB Outputs twice, Why?

Post by requinix »

You're getting the rid from only one table, but your query will be selecting every combination of research rows paired with economy rows. Five rows in each table would mean 25 results. Apparently there are two rows in one of those tables so that will double the number of results from the other table.
Post Reply