0

table:

fungi  |  fruit  |  country

fungi-a       Aple, Orange, Grape,               Japan, America, China, Australia,

fungi-b       Aple, Watermelon, Grape,           Korea, America, China,

fungi-c       Aple, Watermelon, Orange,          Korea, Canada, China, America

fungi-d       Aple, Grape, Orange,               Japan, Canada,

include"config.php";

$result = mysql_query("SELECT * FROM `table` WHERE `fruit` LIKE
'%aple%' AND `country` LIKE'%korea%'");

function asd(){

 while($row=mysql_fetch_array($result)){

 echo $row['country'];

 } } echo implode(',',array_unique(explode(',',asd())));

value is: Korea, America, China, Korea, Canada, China, Amerika

but i want this value: Korea, America, China, Canada,

i make array_unique explode but not worked,

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Feb 01 '13 at 19:10
  • 2
    You need to re-evaluate your code. You should begin by normalizing your database. This will alleviate the need to do any type of `array_unique` manipulation. – Kermit Feb 01 '13 at 19:12

2 Answers2

0

You are not returning any value at asd()

This should work:

function asd() {
    $result = mysql_query("SELECT * FROM `table` WHERE `fruit` LIKE '%aple%' AND `country` LIKE'%korea%'");
    $countries = array();
    while($row=mysql_fetch_array($result)) {
        $countries = array_merge($countries, explode(',', $row['country']));
    }
    return array_unique($countries);
}
echo implode(',', asd());

This will put every 'country' record in the $countries array and return it.

You may also should take a look at OOP and PDO (PHP Data Objects) or MySQLi (MySQL improved).

Nox
  • 339
  • 1
  • 9
0

Try it

include"config.php";

$result = mysql_query("SELECT * FROM `table` WHERE `fruit` LIKE
'%aple%' AND `country` LIKE'%korea%'");

function asd(){

    $myarr = array();

    while($row=mysql_fetch_array($result)){

      if (!in_array($row['country'], $myarr)) {
        $myarr[] = $row['country'];
      }

    }
    return $myarr;
} 

echo implode(",", asd());
Heberfa
  • 441
  • 3
  • 8