views:

99

answers:

2

Say I have a table that has a column which goes

Column B

apple apple apple orange apple orange orange grapes grapes mango mango orange

And I want to query it in such a way that I get a list like so

apple orange grapes mango

How do I do this in PHP SQL? Much thanks.

A: 

Suppose your table is called 'Fruit' and this column is called 'B'. Here's how you would do it:

SELECT DISTINCT B FROM Fruit;

The 'DISTINCT' keyword will get you all unique results. That's the SQL part. In PHP, you write the query like this:

// Perform Query
$query = 'SELECT DISTINCT B FROM Fruit';
$result = mysql_query($query);

// Get result
while ($row = mysql_fetch_array($result)) {
    echo $row['B'];
}
Goose Bumper
Thanks a lot Goose Bumper
Sriram
A: 

Where do you want to do the actual filtering? SQL or PHP?

For SQL:

SELECT DISTINCT foo FROM table;
#or#
SELECT foo FROM table GROUP BY foo;

For PHP:

$foo = array('a','a','b','b','c');
$foo_filtered = array_unique($foo);
Moot
Thanks a lot Moot. There's another thing I learned array. But say I don't even know what the array is gonna be — just values from a table that keeps adding to itself. So what do I do? Much thanks.
Sriram