Page 1 of 1

Help! can't use array_unique

Posted: Sat Mar 10, 2007 12:33 am
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

Posted: Sat Mar 10, 2007 1:43 am
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.

Posted: Sat Mar 10, 2007 3:31 am
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.

Posted: Sun Mar 11, 2007 10:04 am
by hesham2012
Thank you For help