Mysql选择不同的ID和名称

时间:2016-03-09 13:01:37

标签: php mysql

我有一个带有一些字段的MySQL表。 2个字段存在问题。

包含唯一编号的字段official_country_name_sort_field和包含名称的字段“official_country_name”。

我想选择唯一的official_country_name_sort_field并在GET方法中使用它,但在网站上我想显示来自official_country_name的名称。

我有这个函数显示来自official_country_name_sort_field的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;

}

<?php foreach ($officialCountryNames as $officialCountryName ) : ?>
      <h3>  <a href="<?php echo "../currencylist?showall&country=$slug&category=$category&official_name=$officialCountryName"; ?>  "><?php  echo "$officialCountryName"; ?></a></h3>
      

      <?php endforeach  ?>

这个节目,我有“officialCountryName” - ID(表格栏号“official_country_name_sort_field”中的数字。。如何显示表格栏“official_country_name”中的名字而不是“official_country_name_sort_field”中的ID。

1 个答案:

答案 0 :(得分:0)

    $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;

}

使用上述查询并在while循环中进行更改。