Jump to content

Avoid duplicate strings in MySQL output?


bigmax

Recommended Posts

Good day guys. I'm a total beginner with PHP so I might naturally be missing out on some function that is obvious to those familiar with it.  I have a problem -- have to remove duplicate strings from a MYSQL query output...don't know how, using PHP that is.

So if the query displays something like:
[code]
Joe
Joe
James
Jane
Mary
Mary
Mary
John
[/code]

I need it to display only  Joe, James, Jane, Mary, John, using the above analogy. Holy shit....how?? PHP's confusing...

[code]
<?
/* MySQL connection script here...*/
/* connected. */
$town = "Tumbaktu";
$names = mysql_query("SELECT Name FROM my_table WHERE Town=$town");

while($row = mysql_fetch_array($names, MYSQL_NUM))
{
// using these two lines, i get an empty display
$res = array_unique($row);     
print "<br>".$res[0];                 

/*
whereas were i to write simply
print "<BR>".$row[0];
instead, i'd get a list similar to the first one above...
*/
}
?>
[/code]

Kindly help anybody.
Link to comment
https://forums.phpfreaks.com/topic/29140-avoid-duplicate-strings-in-mysql-output/
Share on other sites

You want to do something like the following:
[code]<?php
$town = "Tumbaktu";
$q = "select distinct Name FROM my_table WHERE Town=$town");
$names = mysql_query($q) or die("Problem with query: $q<br>" . mysql_error());

while($row = mysql_fetch_assoc($names))
{
    echo $row['Name'] . '<br>';
}
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.