Quote:
Originally Posted by BeRtjh
SELECT DISTINCT location FROM table_name
I don't think that is quite the answer that is expected. Since this is a PHP forum I assume that you want a PHP solution.
Since you (most of the times) do not know what the result columns will be, you want a solution that is independent of the number of result columns and dynamically builds the resulting output grid.
The following code is a reworked code snippet by
janezr at jcn dot si from the PHP documentation. It takes the result and displays it in a grid at the screen, more or less like the MySQL command interface displays. You can easily adapt that if you want the results to be stored in an array.[php]$result=mysql_query("SELECT players.*, locations.* FROM players
LEFT JOIN locations ON players.id = locations.pid ")
or die ("invalid query: ".mysql_error());
if (mysql_num_rows($result) > 0) {
$numfields = mysql_num_fields($result);
echo "<table>\n<tr>";
// print the header i.e. column names
for ($i=0; $i < $numfields; $i++) {
echo '<th>'.mysql_field_name($result, $i).'</th>';
}
echo "</tr>\n";
// print the data, i.e. column values
while ($row = mysql_fetch_row($result)) {
echo '<tr><td>'.implode($row,'</td><td>')."</td></tr>\n";
}
echo "</table>\n";
}
else
echo 'No results!";[/php]Ronald :cool: