title: Can't execute prepared statements using nested while loops
body: <p>I am having trouble with changing over some code from old style mysql queries to being prepared. I assume the problem is due to the fact that I'm using multiple whiles which each have their own query in which is causing problems cause only one prepared statement can be active at a time.</p>
  
  <p>EDIT: If anyone cares, I've made it work with only 2 loops like so - </p>
  
  <pre><code>function createDeskMenu()
  {
      global $bookingTimes, $dbconn;
      $day0 = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
  
      $query = "SELECT location FROM location";
      $result = mysqli_query($dbconn,$query);
      mysqli_num_rows($result);
      while ($row = mysqli_fetch_array($result))
      {
          $location = $row['location'];
          echo "&lt;h3&gt;$location&lt;/h3&gt;&lt;div&gt;";
          $query = $dbconn -&gt; prepare("SELECT COALESCE( CountDesk, 0 ) total, name, d.desk_id, phone, fax, dock, pc FROM desk d LEFT JOIN (SELECT COUNT(booked.desk_id) CountDesk, desk_id FROM booked WHERE booking_id &gt;=?)b ON d.desk_id = b.desk_id WHERE location=?");
          $query-&gt;bind_param("is",$day0, $location);
          $query-&gt;execute();
          $query-&gt;bind_result($totalCount,$name,$desk_id,$phone,$fax,$dock,$pc);
          while($query-&gt;fetch()) {
              $total = count($bookingTimes) * 14 - $totalCount;
              echo '&lt;a href="?page=desk&amp;desk='.$desk_id.'"&gt;&lt;div class="desk"&gt;&lt;b&gt;'.$name.' 
              ('.$total.' Available Bookings)&lt;/b&gt;&lt;li&gt;Facilities:';
              if($phone){echo " Phone,";}if($fax){echo " Fax Machine,";}if($dock){echo " Laptop Dock.";}if($pc){echo " Desktop Workstation.";}
              echo '&lt;/li&gt;&lt;/div&gt;&lt;/a&gt;&lt;hr /&gt;';
          }
          $query-&gt;close();
          echo '&lt;/div&gt;';
      }
  }
  </code></pre>