2016-03-26 392 views
2

我有這樣的代碼,這是一個奇怪的事情,但是當我從第一個查詢中得到的last_name是一個唯一的數字,並得到了我的幫助來自另一個表的姓氏,但當我嘗試獲取另一個數據時,第二個數據結束時它認爲第一個while循環已結束,然後我無法從第一個while循環獲取其餘數據,我甚至嘗試過代碼,但不幸的是它並沒有爲我工作如何從While循環中獲取數據while while循環

<div class="row"> 
    <?php 
    $sql2 = "SELECT id,last_name,age,height,weight,gender,education,work 
      from profile where parent_key!='$parent_key' "; 
    $retval2 = mysqli_query($con,$sql2); 

    if(! $retval2) { 
     die('Could not get data: ' . mysql_error()); 
    } 

    while($row = mysqli_fetch_array($retval2, MYSQL_ASSOC)) { 
    ?> 

    <div class="row"> 
     <div class="col-lg-6 col-md-6 col-sm-6"> 
      <span>Unique Id</span> 
      <h3><?php echo $row['id'] ?></h3> 
     </div> 
    <div class="col-lg-6 col-md-6 col-sm-6"> 
     <span>Surname</span> 
     <h3><?php $surname = $row['last_name']; 
      $sql3 = "SELECT last_name from surname where id='$surname'"; 
      $retval3 = mysqli_query($con,$sql3); 
      if(! $retval3) { 
       die('Could not get data: ' . mysql_error()); 
      } 
      while($row = mysqli_fetch_array($retval3, MYSQL_ASSOC)) { 
       echo $new_last_name ="{$row['last_name']}"; 
      ?> 
      ?> 
     </h3> 
    </div> 

</div> 
<!-- /.row --> 


<!-- /.row --> 

<?php 
    } 
?> 

<?php 
    } 
?> 
</div> 
<!-- /.row --> 

並感謝您的幫助提前。

+0

看起來好像是你內心的右括號while循環是出來的地方也。 –

回答

0

內第二while循環只是改變:

$row = mysqli_fetch_array($retval3, MYSQL_ASSOC) 

$row1 = mysqli_fetch_array($retval3, MYSQL_ASSOC) 

,那麼你可以先使用值,而與$row['columnname']和第二,而與$row1['columnname']

+0

感謝Marco幫助我出去 –

+0

@ParthDhorda這是我的榮幸。 – kunicmarko20

1

你可以使用單個查詢從其他表中獲取用戶名和姓氏(姓氏)

 $sql2 = "SELECT surname.last_name as surname,profile.id as id,profile.last_name,profile.age,profile.height,profile.weight,profile.gender,profile.education,profile.work 
        from profile 
left join surname on surname.id=profile.last_name 
    where parent_key!='$parent_key' "; 

現在你不需要兩次編寫代碼來

全碼取姓以下

<div class="row"> 
       <?p$sql2 = "SELECT surname.last_name as surname,profile.id as id, 
           profile.last_name,profile.age,profile.height,profile.weight,profile.gender,profile.education, 
           profile.work 
         from profile 
          left join surname on surname.id=profile.last_name 
          where parent_key!='$parent_key' "; 
        $retval2 = mysqli_query($con,$sql2); 

        if(! $retval2) 
        { 
         die('Could not get data: ' . mysql_error()); 
        } 



        while($row = mysqli_fetch_array($retval2, MYSQL_ASSOC)) 
        { 
         ?> 

         <div class="row"> 
          <div class="col-lg-6 col-md-6 col-sm-6"> 
           <span>Unique Id</span> 
           <h3><?php echo $row['id'] ?></h3> 
          </div> 
          <div class="col-lg-6 col-md-6 col-sm-6"> 
           <span>Surname</span> 
           <h3><?php echo $row['surname'];  ?>         

           </h3> 
          </div> 

         </div> 


        <?php 
         } 
        ?> 
      </div> 
+0

感謝Ajay幫忙,因爲我可以從這兩個代碼中獲取數據。 再次向Marco致謝 –

+0

請提出答案。謝謝 – Ajay