Page 1 of 1

DB Outputs twice, Why?

Posted: Tue Nov 06, 2012 6:48 am
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

Re: DB Outputs twice, Why?

Posted: Tue Nov 06, 2012 7:17 am
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.

Re: DB Outputs twice, Why?

Posted: Tue Nov 06, 2012 7:49 am
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

Re: DB Outputs twice, Why?

Posted: Tue Nov 06, 2012 1:34 pm
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.