Page 1 of 1
Duplicate results
Posted: Tue Sep 07, 2004 5:30 am
by Perfidus
I'm queryng mysql database:
$result= mysql_query("SELECT * FROM $table" ,$link);
while ($row = mysql_fetch_array($result)){
And then I try to echo results:
echo "<br>";
echo $row['whatever'];
echo $row['whatever2'];
echo "<br>";
}
But I get the results repeted as much times as lines in my table!!
How can avoid that?
Posted: Tue Sep 07, 2004 5:36 am
by feyd
that's exactly what you asked for... you may want to look into [mysql_man]LIMIT[/mysql_man]
Posted: Tue Sep 07, 2004 5:49 am
by CoderGoblin
Normally for a SELECT statement you would also have a WHERE clause.
Code: Select all
SELECT * FROM TABLE WHERE column<10;
Another useful command if duplicate data exists is DISTINCT
The question you have to ask yourself however is why does the table contain duplicates ?
Posted: Tue Sep 07, 2004 8:03 am
by Perfidus
I'm sorry I didn't explain myself pretty good.
Lets say I have this table:
num | weight | size | age |
----------------------------------
1 | 45 | 1,60 | 17 |
----------------------------------
2 | 56 | 1,78 | 19 |
----------------------------------
3 | 47 | 1,67 | 18 |
-----------------------------------
I try to get size and age from all the table, and I get them but like this:
1,60 - 17
1,60 - 17
1,60 - 17
1,78 - 19
1,78 - 19
1,78 - 19
1,67 - 18
1,67 - 18
1,67 - 18
Instead this:
1,60 - 17
1,78 - 19
1,67 - 18
Posted: Tue Sep 07, 2004 8:18 am
by CoderGoblin
DISTINCT should solve your problem. If all the columns are the same, only one row is returned rather than the duplicates. If only one column is different then DISTINCT doesn't work. There is syntax for DISTINCT ON but you would need to look that up. An alternative is to limit the returned rows to contain only the information required.
Code: Select all
SELECT DISTINCT weight,size,age FROM mytable;
This has the added advantage that the return size is smaller. (For complex selects it is noticable how much quicker returning only those values you need can be.)
Have you tried your SELECT yourself using SQL ? Ensure the SQL is correct first, then handle it with PHP.