text: Q:
  
  Preventing MySQL Queries in PHP while loops? (Use one instead)
  
  Here are my tables...
  Locations
  __________________________________________________________
  ID          Location           Region             Country
  
  1           Newmarket          Ontario            Canada
  2           Barrie             Ontario            Canada
  3           New York City      New York           US
  4           Buffalo            New York           US
  5           Vancouver          British Columbia   Canada
  
  Regions
  __________________________________________________________
  ID          Region               Country
  
  1           Ontario              Canada
  2           New York             US
  3           British Columbia     Canada
  
  Countries
  __________________________________________________________
  ID           Country
  
  1            Canada
  2            US
  
  The tables are much bigger than this and I am trying to load the lists into an expandable list like so...
  
  The styling is no problem, I already have that sorted out. I am just attempting to load all this information in one fast MySQL Query. This is what I currently have in PHP and it takes a LOT of queries with these while loops. (With the size of my tables)
      <div id="listContainer">
          <ul id="expList">
                  <?php
                  $countries = $conn->query("SELECT * FROM countries ORDER BY country ASC");
                  while($crow = mysqli_fetch_array($countries))
                  {   
                      $cname = $crow['country'];
                      $regions = $conn->query("SELECT * FROM regions WHERE country = '$cname' ORDER BY region ASC")
                      ?>
                      <li class="country">
                      <?php echo $cname; ?>
                          <ul class="undercountry">
                                  <?php
                                  while($rrow = mysqli_fetch_array($regions))
                                  {
                                  echo "<li>";
                                  $rname = $rrow['region'];
                                  echo $rname;
                                  ?>
                                      <ul>
                                          <?php
                                          $locations = $conn->query("SELECT * FROM locations WHERE region = '$rname' AND country = '$cname' ORDER BY location ASC");
                                          while($lrow = mysqli_fetch_array($locations)){
                                              $lname = $lrow['location'];
                                              echo $lname;
                                          }
                                          ?>
                                      </ul>
                                  <?php
                                  echo "</li>";
                                  }
                                  ?>
                          </ul>
                      </li>
                      <?php
                  }
                  ?>
              </ul>
          </div>
  
  Is there a way to do this? It's okay to only use the locations table and group but I'm not too good at writing select queries.
  
  A:
  
  Since your locations table has all the information of country and regions as well, you don't need to use the other tables. You can run a query like this:
  SELECT * FROM locations ORDER BY country, region
  
  This will return you an array sorted by country, and then region. You can then loop over this to create your select element. Here's an example that prints out the results as you need. Modify it as needed:
  <?php
  
  // example results
  $results = array(
      array(
          'Location' => 'Vancouver',
          'Region' => 'British Columbia',
          'Country' => 'Canada'
      ),
      array(
          'Location' => 'New Market',
          'Region' => 'Ontario',
          'Country' => 'Canada'
      ),
      array(
          'Location' => 'Barrie',
          'Region' => 'Ontario',
          'Country' => 'Canada'
      ),
      array(
          'Location' => 'New York City',
          'Region' => 'New York',
          'Country' => 'US'
      )
  );
  
  $i = 0;
  
  while ($i < count($results)) {
      $currentCountry = $results[$i]['Country'];
      echo "-" . $currentCountry . PHP_EOL;
  
      while ($results[$i]['Country'] == $currentCountry) {
          $currentRegion = $results[$i]['Region'];
          echo "--" . $currentRegion . PHP_EOL;
  
          while ($results[$i]['Region'] == $currentRegion) {
              echo "---" . $results[$i]['Location'] . PHP_EOL;
              $i++;
              if ($i == count($results)) break;
          }
  
          if ($i == count($results)) break;       
      }   
  }
  
  This way you only need to run a single SQL query.
  Demo
  
  