简体   繁体   English

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

[英]Store MySQL results in PHP array for two queries

How do I create, store and output an array that uses two different mysql queries? 如何创建,存储和输出使用两个不同的mysql查询的数组?

I tried to make a simple example. 我试图举一个简单的例子。

$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

I do not know if you are checking for errors, preparing or escaping your queries but please do so. 我不知道您是要检查错误,准备还是转义查询,但请这样做。

To generate your array you can do it with this: 要生成您的数组,您可以这样做:

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

To display your array you can do: 要显示数组,您可以执行以下操作:

    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 />';
    }

Also, if you know the country id or city id you can do: 另外,如果您知道国家/地区ID或城市ID,则可以执行以下操作:

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

Use your country_id as the key for your array.. So basically, 使用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']
  }
}

In order to print these back out using foreach (as you asked in comments), you must loop through both levels of the array 为了使用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/>';
   }

}

Taking the output of one query and mailmerging it into another query in a nested loop is an anti-pattern. 将一个查询的输出以嵌套循环的形式邮寄并合并到另一个查询中是一种反模式。 Use a join: 使用联接:

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

Then.... 然后....

$cities=array();
$country_names=array();

While($r=mysqli_fetch_array($handle)) {
     $country_names[$r['country_id']]=$r['country_name'];
     If (!is_array($cities[$r['country_id'])) $cities[$r['country_id']]=array();
     $cities[$r['country_id']][$r['city_id']]=$r['city_name'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM