将MySQL结果存储在PHP数组中以进行两次查询

时间:2016-03-30 14:34:52

标签: php mysql arrays

如何创建,存储和输出使用两个不同mysql查询的数组?

我试着做一个简单的例子。

$select1 = "SELECT country_id, country_name FROM countries ...";
while ($select1) {
   ...store country results in array...

  $select2 = "SELECT city_id, city_name FROM cities where '" . $select1['country_id'] . "'..."); // depends on select1
  while ($select2) {
    ...store city results in array...
  }

}

**output something like this:**

country_id = 1
country_name = United States

  city_id = 1
  city_name = New York

  city_id = 2
  city_name = Las Vegas

country_id = 2
country_name = Canada

  city_id = 3
  city_name = Ottawa

3 个答案:

答案 0 :(得分:2)

我不知道您是否正在检查错误,准备或转发您的查询,但请注意。

要生成阵列,您可以使用此方法:

    $list = [];
    $countries = $link->query("SELECT country_id, country_name FROM countries ...");

    while ($country_row /*fetch from $countries*/) {

        $country_id = $country_row['country_id']; 

        $country_info = [
                'country_id' => $country_id,
                'country_name' => $country_row['country_name'],
                'country_cities' => []
         ];

        $cities_stmt = "SELECT city_id, city_name FROM cities where $country_id...";
        $cities = $link->query($cities_stmt);

        while ($city_row /*fetch from $cities*/) {

            $city_id = $city_row['city_id'];

            $country_info['country_cities'][$city_id] = [
                    'city_id' => $city_id,
                    'city_name' => $city_row['city_name']
            ];
        }

        $list[$country_id] = $country_info;
    }

要显示阵列,您可以这样做:

    foreach ( $list as $country_id => $country_info ) {

        echo "Country ID: $country_id<br />";
        echo 'Country Name: ' . $country_info['country_name'] . '<br />';
        echo 'Country Cities:<br />';

        $cities = $country_info['country_cities']; 

        foreach ( $cities as $city_id => $city_info ) {

                echo "   City ID: $city_id<br />";
                echo '   City Name: ' . $city_info['city_name'] . '<br />';
        }

        echo '<br />';
    }

此外,如果您知道国家/地区ID或城市ID,则可以执行以下操作:

    echo 'City Name: ' . $list[$country_id]['country_cities'][$city_id]['city_name'] . '<br />';

答案 1 :(得分:0)

使用country_id作为数组的键。基本上,

$select1 = mysqli_query("SELECT country_id, country_name FROM countries");
while ($country_row = mysqli_fetch_array($select1, MYSQLI_ASSOC)) {
   $array[$country_id]['country_id'] = $country_row ['country_id'];
   $array[$country_id]['country'] = $country_row['country_name'];

  $select2 = mysqli_query("SELECT city_id, city_name FROM cities where '" . $select1['country_id'] ."' "); // depends on select1
  while ($city_row = mysqli_fetch_array($select2, MYSQLI_ASSOC)) {
   $array[$country_id]['cities'][]['city_id'] = $city_row['city_id'];
   $array[$country_id]['cities'][]['city_name'] = $city_row['city_name']
  }
}

为了使用foreach打印这些(如您在评论中所述),您必须遍历数组的两个级别

foreach($array as $country){
   //Loop through the countries that we queried and stored 
   echo 'Country Name: '.$country['country_name'].'<br/>';
   echo 'Country ID: '.$country['country_id'].'<br/>';
   echo 'Cities in country: <br/>';
   //Loop through the cities within this country
   foreach($country['cities'] as $city){
      echo 'City ID: '.$city['city_id'].'<br/>';
      echo 'City Name: '.$city['city_name'].'<br/>';
   }

}

答案 2 :(得分:0)

获取一个查询的输出并将其邮寄到嵌套循环中的另一个查询中是一种反模式。使用联接:

public class B : A {
    public float SomeProperty {
        get {
            return SomeValue;
        }
    }
}

然后...

select country_ID, country_name,
  City_Id, city_name
From countries cn
Inner join cities ct
On CT.country_id = cn.country_ID