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())));
值为:
韩国、美国、中国、韩国、加拿大、中国、美国但我想要这个值:韩国、美国、中国、加拿大、
我让array_unique爆炸但没有工作,
你没有在 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改进)。
试试吧
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());