title: PDO query run only first time
body: <p>I'm trying to get info from database but the query runs only for the first row!
  I have tried many things but it didn't work; I don't know why.
  The get method is on table that is filled by the database</p>
  
  <p>The issue is in the first query <code>'SELECT CouID FROM CoursesC WHERE StuID = :ID '</code>
  if I change the query to <code>'SELECT CouID FROM CoursesC'</code> it works fine!</p>
  
  <p>I'm presenting the data into a <code>&lt;select&gt;</code> inside a modal</p>
  
  <p><strong>FORM CODE</strong> </p>
  
  <pre><code>&lt;form action="&lt;?php $_SERVER['PHP_SELF']?&gt;" method="GET" id="contact"&gt;
  
  &lt;table  id="studenttable"&gt;
  
    &lt;tr&gt;
    &lt;th&gt;&lt;/th&gt;
    &lt;th&gt;Student ID&lt;/th&gt;
    &lt;th&gt;Student Name&lt;/th&gt;
    &lt;th&gt;Student Passed Credits&lt;/th&gt;
      &lt;th&gt;Action&lt;/th&gt;
    &lt;/tr&gt;
    &lt;?php
  
      require('check.php');
  $query = $conn-&gt;prepare('SELECT * FROM student WHERE AdvID=:id' );
  $query-&gt;bindParam(":id",$_SESSION['userid']);
  $query-&gt;execute();
  $row = $query-&gt;fetch();
  
  while($row = $query-&gt;fetch(PDO::FETCH_ASSOC)){
   ?&gt;
    &lt;tr&gt;
    &lt;td&gt; &lt;input type ="checkbox" name="checked[]" value="&lt;?php echo $row['StuId'];?&gt;"&gt; &lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['StuId']; ?&gt;&lt;/td&gt;
    &lt;td&gt;&lt;?php echo $row['Name']; ?&gt;&lt;/td&gt;
    &lt;td&gt; &lt;/td&gt;
    &lt;td&gt; 
  
    &lt;button  type="submit" name="submit" value="&lt;?php echo $row['StuId'];?&gt;" class="btn icon-btn btn-info" &gt;&lt;span class="glyphicon glyphicon-info-sign"&gt;&lt;/span&gt;More Information&lt;/button&gt;
    &lt;/td&gt;
    &lt;/tr&gt;    
  &lt;?php
  }
        do {
      while ($query-&gt;fetch())
          ;
      if (!$query-&gt;nextRowset())
          break;
  } while (true);
      $conn=null;
      $query-&gt;closeCursor();
  ?&gt; 
  
  &lt;/table&gt;
  
  &lt;/form&gt;
  
  
  
  
  &lt;?php
  if (isset($_GET['submit'])){
      global $CC;
      $count=1;
      global $data;
      $T=$_GET['submit'];
  ?&gt;
  &lt;script&gt;
      $('#CC').html("");
      $('#CC1').html("");
  &lt;/script&gt;
  &lt;?php
      require('check.php');
      $q= $conn-&gt;prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
      $q-&gt;bindValue(':ID', $_GET['submit'] );
      $q-&gt;execute();
      $data = $q-&gt;fetch();
      while($data = $q-&gt;fetch(PDO::FETCH_ASSOC)){
          $CC=$data['CouID'];
  ?&gt;
  &lt;script&gt;
      $('#CC').append("&lt;option&gt;&lt;? echo $count++; echo "- "; echo $CC;?&gt;&lt;/option&gt;");
  &lt;/script&gt;
  &lt;?php
      }
      $q-&gt;closeCursor();
      $A=$CC;
      /*echo "&lt;script&gt; alert('&lt;?php $T ?&gt;')";*/
  
      $q1= $conn-&gt;prepare('SELECT CouID FROM Courses');
      $q1-&gt;execute();
      $data1 = $q1-&gt;fetch();
      while($data1 = $q1-&gt;fetch(PDO::FETCH_ASSOC)){
          if($CC!=$data1['CouID']){
  ?&gt;
  &lt;script&gt;
      $('#CC1').append("&lt;option&gt;&lt;? echo $count++; echo "- "; echo $data1['CouID'];?&gt;&lt;/option&gt;");
  &lt;/script&gt;
  &lt;?php
          }
      }
      $q1-&gt;closeCursor();
  ?&gt;
  &lt;script&gt;
      $(window).load(function(){
          $('#myModal').modal('show');
      });
  &lt;/script&gt;
  &lt;?
  }
  ?&gt;
  &lt;?php
      if (isset($_GET['submit'])){
          global $CC;
          $count=1;
          global $data;
          $T=$_GET['submit'];
  ?&gt;
  &lt;script&gt;
       $('#CC').html("");
       $('#CC1').html("");
  &lt;/script&gt;
  &lt;?php
          require('check.php');
          $q= $conn-&gt;prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
          $q-&gt;bindValue(':ID', $_GET['submit'] );
          $q-&gt;execute();
          $data = $q-&gt;fetch();
          while($data = $q-&gt;fetch(PDO::FETCH_ASSOC)){
              $CC=$data['CouID'];
  ?&gt;
  &lt;script&gt;
       $('#CC').append("&lt;option&gt;&lt;? echo $count++; echo "- "; echo $CC;?&gt;&lt;/option&gt;");
  &lt;/script&gt;
  &lt;?php
          }
          $q-&gt;closeCursor();
          $A=$CC;
          /*echo "&lt;script&gt; alert('&lt;?php $T ?&gt;')";*/
  
          $q1= $conn-&gt;prepare('SELECT CouID FROM Courses');
          $q1-&gt;bindParam(":id", $_GET['submit']);
          $q1-&gt;execute();
          $data1 = $q1-&gt;fetch();
          while($data1 = $q1-&gt;fetch(PDO::FETCH_ASSOC)){
              if($CC!=$data1['CouID']){
  ?&gt;
  &lt;script&gt;
       $('#CC1').append("&lt;option&gt;&lt;? echo $count++; echo "- "; echo $data1['CouID'];?&gt;&lt;/option&gt;");
  &lt;/script&gt;
  &lt;?php
              }
          }
          $q1-&gt;closeCursor();
  ?&gt;
  &lt;script&gt;
      $(window).load(function(){
          $('#myModal').modal('show');
      });
  &lt;/script&gt;
  &lt;?
      }
  ?&gt;
  &lt;?php
      if (isset($_GET['submit'])){
        global $CC;
          $count=1;
          global $data;
          $T=$_GET['submit'];
      ?&gt;
      &lt;script&gt;
       $('#CC').html("");
       $('#CC1').html("");
      &lt;/script&gt;
      &lt;?php
      require('check.php');
      $q= $conn-&gt;prepare('SELECT CouID FROM CoursesC WHERE StuID = :ID ');
      $q-&gt;bindValue(':ID', $_GET['submit'] );
      $q-&gt;execute();
      $data = $q-&gt;fetch();
      while($data = $q-&gt;fetch(PDO::FETCH_ASSOC)){
          $CC=$data['CouID'];
  ?&gt;
  &lt;script&gt;
       $('#CC').append("&lt;option&gt;&lt;? echo $count++; echo "- "; echo $CC;?&gt;&lt;/option&gt;");
  &lt;/script&gt;
  &lt;?php
      }
      $q-&gt;closeCursor();
      $A=$CC;
      /*echo "&lt;script&gt; alert('&lt;?php $T ?&gt;')";*/
  
      $q1= $conn-&gt;prepare('SELECT CouID FROM Courses');
      $q1-&gt;bindParam(":id", $_GET['submit']);
      $q1-&gt;execute();
      $data1 = $q1-&gt;fetch();
      while($data1 = $q1-&gt;fetch(PDO::FETCH_ASSOC)){
          if($CC!=$data1['CouID']){
  ?&gt;
  &lt;script&gt;
       $('#CC1').append("&lt;option&gt;&lt;? echo $count++; echo "- "; echo $data1['CouID'];?&gt;&lt;/option&gt;");
  &lt;/script&gt;
  &lt;?php
          }
      }
      $q1-&gt;closeCursor();
  ?&gt;
  &lt;script&gt;
      $(window).load(function(){
          $('#myModal').modal('show');
      });
  &lt;/script&gt;
  &lt;?
  }
  ?&gt;
  </code></pre>