0

桌子:

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())));

值为:韩国、美国、中国、韩国、加拿大、中国、美国

但我想要这个值:韩国,美国,中国,加拿大,

我让 array_unique 爆炸但没有用,

4

2 回答 2

0

您没有在 asd() 返回任何值

这应该有效:

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());

这会将每个“国家”记录放入 $countries 数组并返回它。

您可能还应该看看 OOP 和 PDO(PHP 数据对象)或 MySQLi(改进的 MySQL)。

于 2013-02-01T19:12:24.383 回答
0

试试看

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());
于 2013-02-01T19:20:31.100 回答