title: Two PDO sql statements/Using fetched data for the second statement
body: <p>So I'm new to PHP/PDO and I'm having some problems with passing the fetched variable on to the second statement. I'm also having problems with exceptions i don't know how the right structure for this situation. </p>
  
  <pre><code>try {   
  $connection-&gt;beginTransaction();
  $stmt = $connection-&gt;prepare("CALL sproc_patient_profile_physical_exam_hdr(?,?,?)");
  $stmt-&gt;bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
  $stmt-&gt;bindValue(2,$patientid_val,PDO::PARAM_INT);  
  $stmt-&gt;bindValue(3,$enteredby,PDO::PARAM_STR);
  
  
  while($row = $stmt-&gt;fetch()){
      echo $physicalexamid_insert=$row['physicalexamid'];       
      /* I need to use this data for another try and catch or sql statements for example*/
  
      $count_physical_exam_id = count($physical_exam_id);
      for ($x=0; $x &lt; $count_physical_exam_id; $x++) { 
          if (!(empty($physical_exam_id[$x]))) {
             try {    
              $connection-&gt;beginTransaction();
              $stmt = $connection-&gt;prepare("CALL sproc_patient_profile_physical_exam_dtl(?,?,?,?,?,?,?)");
              $stmt-&gt;bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
              $stmt-&gt;bindValue(2,1,PDO::PARAM_INT);   
              $stmt-&gt;bindValue(3,$physical_exam_id[$x],PDO::PARAM_INT);
              $stmt-&gt;bindValue(4,$physical_exam_desc[$x],PDO::PARAM_STR);
              $stmt-&gt;bindValue(5,$normal[$x],PDO::PARAM_INT);
              $stmt-&gt;bindValue(6,$undone[$x],PDO::PARAM_INT);
              $stmt-&gt;bindValue(7,$specific[$x],PDO::PARAM_INT);
  
              $stmt-&gt;execute();   
              $connection-&gt;commit();
  
              } catch(PDOException $ex) { 
              //$connection-&gt;rollBack();
              echo $ex-&gt;getMessage();
  
          }
  
          }
      }
  
  } 
  $stmt-&gt;execute();   
  $connection-&gt;commit();       
  } catch(PDOException $ex) { 
  $connection-&gt;rollBack();
  echo $ex-&gt;getMessage();
  </code></pre>
  
  <p>Can someone help me with this? Thanks.</p>