Php Mysql选择不同的ID和名称
我有一个带有一些字段的MySQL表。两个字段有问题 字段是包含唯一数字的Php Mysql选择不同的ID和名称,php,mysql,Php,Mysql,我有一个带有一些字段的MySQL表。两个字段有问题 字段是包含唯一数字的official\u country\u name\u sort\u字段,以及包含名称的字段“official\u country\u name” 我想选择唯一的official\u country\u name\u sort\u字段,并在GET方法中使用它,但在站点上我想显示official\u country\u name中的名称 我有一个函数,显示来自官方\国家\名称\排序\字段的id: $sql = "select
official\u country\u name\u sort\u字段
,以及包含名称的字段“official\u country\u name”
我想选择唯一的official\u country\u name\u sort\u字段
,并在GET方法中使用它,但在站点上我想显示official\u country\u name
中的名称
我有一个函数,显示来自官方\国家\名称\排序\字段的id
:
$sql = "select distinct official_country_name_sort_field as name from t_container,t_country where (t_container.country_id = t_country.id) and (t_container.category = '$category') and (t_country.slug = '$slug') order by t_container.official_country_name_sort_field";
$result = mysqli_query($link,$sql);
if (!$result) {
$output = 'Error fetching official country names : '.mysqli_error($link);
include '../templates/db/error.html.php';
exit();
}
$officialCountryNames = array();
while ($row= mysqli_fetch_array($result)) {
$officialCountryNames [] = $row['name'];
}
return $officialCountryNames;
}
使用上述查询并在while循环中进行更改。将该字段添加到查询中。我知道这一点。这就是以前的情况。当我使用“官方国家名称”时,我得到了名称,但名称不是唯一的。有些名字是一样的。此字段“官方\国家\名称\排序\字段”具有唯一值(数字)。这就是为什么我需要使用它来选择数据,但我需要显示名称而不是数字谢谢您的回复。我更改了它,但当我更改为时,我收到了通知“数组到字符串的转换…”,然后我收到了“通知:未定义索引:名称…”
$sql = "select distinct official_country_name_sort_field as code, official_country_name as name from t_container,t_country where (t_container.country_id = t_country.id) and (t_container.category = '$category') and (t_country.slug = '$slug') order by t_container.official_country_name_sort_field";
$result = mysqli_query($link,$sql);
if (!$result) {
$output = 'Error fetching official country names : '.mysqli_error($link);
include '../templates/db/error.html.php';
exit();
}
$officialCountryNames = array();
while ($row= mysqli_fetch_array($result)) {
$officialCountryNames[]['name'] = $row['name'];
$officialCountryNames[]['code'] = $row['name'];
}
return $officialCountryNames;
}