Help! can't use array_unique

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hesham2012
Forum Newbie
Posts: 8
Joined: Fri Mar 09, 2007 11:34 pm

Help! can't use array_unique

Post by hesham2012 »

Here is the a problem i have a database in the table links there is a row 'name' this row contais a several names but some of them are similar i want to show this row results without repeating i do like this

Code: Select all

$con=mysql_connect("localhost","","");
mysql_select_db("db",$con);
$query=("select * from links ");
$result=mysql_query($query);

while ($row=mysql_fetch_assoc($result)){
echo $row[name]."<br>"  ;
}
but i don't know how to use 'array_unique ' to diable repeating
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

In this case I would suggest using DISTINCT within the sql query
http://dev.mysql.com/doc/refman/5.0/en/select.html

Other than that you could check on http://bg.php.net/array_unique
to check on how to use array_unique.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

also

Code: Select all

echo $row[name]."<br />";
should be

Code: Select all

echo $row['name']."<br />";
note the single quotes around 'name'. Although it will work at present the way you have it, PHP will be issuing warnings for every call to this. The PHP behaviour may change in the future so I would suggest doing it properly now.

Also if only interested in one field from your table then just include that field in the SELECT clause

Code: Select all

$Query = "SELECT DISTINCT(name) FROM links";
No point in giving mysql more work that it needs to do.
hesham2012
Forum Newbie
Posts: 8
Joined: Fri Mar 09, 2007 11:34 pm

Post by hesham2012 »

Thank you For help
Post Reply