title: Executing a Query within another Query
body: <p>I am trying to execute an Sql query that relies on the output of another sql query for each loop. </p>
  
  <p>I have 3 tables books, students and borrowed.</p>
  
  <p>Books table has the full details of all books (b_id is primary key), student has full details of all students (s_id as primary key)  and borrowed is a table which has the s_id and b_id as foreign keys and each entry is created when a student borrows a book. </p>
  
  <p>The relationship is one to many i.e Student can borrow one or many books or none but a book cannot be with more than one student. As such my b_id is unique in the table index. </p>
  
  <p>My plan is to create a Html table that lists all students that have borrowed books and on clicking a student in this table a drop down table shows all the books borrowed by that student. </p>
  
  <p>I  just can't seem to get the SQL query to run as I need the s_id from the first Sql query to get the next one to work. plus my drop down table isn’t functioning properly either. </p>
  
  <p>Please help see code below</p>
  
  <p></p>
  
  <pre><code>Click on a student to see books borrowed:
  &lt;table class="table2"&gt;
      &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;&lt;/th&gt;
          &lt;th&gt;Name&lt;/th&gt;
          &lt;th&gt;Mat number/th&gt;
          &lt;th&gt;Dept&lt;/th&gt;
          &lt;th&gt;level&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;?php
          $sql_query = "SELECT student.* FROM student, borrowed where student.s_id = borrowed.s_id Group by student.s_id;";
  
          $result =  $db-&gt;query($sql_query);
  
          if(mysqli_num_rows($result)&gt;0){
              $counter = 0;
  
              while ($row = $result-&gt;fetch_array())
              {
                  $counter++;
      ?&gt;
  
      &lt;tr&gt;
          &lt;td class="btn"&gt;&lt;?php echo $counter;?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $row['name'];?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $row['matNo'];?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $row['Dept'];?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $row['level'];?&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;th class="expand1"&gt;&lt;/th&gt;
          &lt;th class="expand1"&gt;Title&lt;/th&gt;
          &lt;th class="expand1"&gt;Author&lt;/th&gt;
          &lt;th class="expand1"&gt;Genre&lt;/th&gt;
          &lt;th class="expand1"&gt;ISBN&lt;/th&gt;
      &lt;/tr&gt;
     &lt;tr&gt;&lt;?php
                 foreach($row as $studentid){
  
                     $sql = "SELECT books.* 
                             FROM books 
                               inner Join borrowed on books.b_id = borrowed.b_id  
                             WHERE borrowed.s_id = $studentid";
  
                     $result2= $dbs-&gt;query($sql);
                     while( $row2 = $result2-&gt;fetch_array()){
          ?&gt;
          &lt;td class="expand1"&gt;&lt;?php echo $row2['title'];?&gt;&lt;/td&gt;
          &lt;td class="expand1"&gt;&lt;?php echo $row2['author'];?&gt;&lt;/td&gt;
          &lt;td class="expand1"&gt;&lt;?php echo $row2['genre'];?&gt;&lt;/td&gt;
          &lt;td class="expand1"&gt;&lt;?php echo $row2['isbn'];?&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;?php
                     }
                 }
          }
      }
          $result-&gt;close();
          $dbs-&gt;close();
      ?&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>This is my Script to create a toggle/collapsible table:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function(){
          $(".btn").click(function(){
              $(".expand1").toggle();
          });
          $(".btn2").click(function(){
              $(".expand2").toggle();
          });
      })
  
  &lt;/script&gt;
  </code></pre>