如何从while循环内的while循环中获取数据

时间:2016-03-26 15:21:30

标签: php mysql

我有这个代码和东西这很奇怪但是当我从第一个查询中得到last_name是一个唯一的数字并且借助于我从另一个表中获得姓氏的拼写但是当我试图得到另一个数据在第二个结束时它结束它认为第一个while循环已经结束然后我不能从第一个while循环得到其余的数据我甚至尝试了这个代码但不幸的是它对我不起作用< / p>

<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 -->

感谢您提前帮助。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

在第二个while循环内部改变:

$row = mysqli_fetch_array($retval3, MYSQL_ASSOC)

$row1 = mysqli_fetch_array($retval3, MYSQL_ASSOC)

然后您可以使用$row['columnname']时的值和$row1['columnname']时的第二个值