在获取之前打印数据

时间:2017-07-24 04:42:25

标签: php mysql

以下是我的代码。

<?php
            if(isset($_POST['Generate']))
            {
                $emp = mysql_real_escape_string($_POST['employee']);
                $startD = $_POST['from'];
                $endD = $_POST['to'];
                $search = "SELECT leaveregister.*, users.first_name, users.last_name FROM leaveregister INNER JOIN users ON leaveregister.employee=users.id WHERE employee = '$emp' AND startDate >= '$startD' AND endDate <= '$endD'";
                $dosearch = mysql_query($search);
                if(mysql_num_rows($dosearch) > 0)
                {
                    echo '<span>Employee : <strong>'.$row['first_name'].'</strong></span>&nbsp;&nbsp;&nbsp;<span>From : <strong>'.$startD.'</strong></span>&nbsp;&nbsp;&nbsp;<span>To : <strong>'.$endD.'</strong></span><br><br><br>' ;
                    echo '<table border="1" class="table table-bordered">';
                    echo '<thead><th>Emp</th><th>Leave Type</th><th>Leave Duration</th><th>Start</th><th>End</th><th>Remarks</th></thead>';
                    echo '<tbody>';
                    while($row = mysql_fetch_array($dosearch))
                    {
                        echo '<tr><td>'.$row['employee'].'</td><td>'.$row['leaveType'].'</td><td>'.$row['leaveDays'].'</td><td>'.$row['startDate'].'</td><td>'.$row['endDate'].'</td><td>'.$row['remarks'].'</td></tr>';
                        }
                    echo '</tbody></table>';
                    }
                    else
                    {                       
                    echo "Not Found!!";
                    }
                }
        ?>

查询会生成给定员工的一些休假详情 我使用INNER JOIN来获取first_name&amp;来自last_name表的users

问题是,我想在表内查询之前打印Employee的First Name 实际上,我想在第二个if之后打印它。

echo '<span>Employee : <strong>'.$row['first_name'].'</strong></span>

我知道我不能在那里使用$row
那怎么打印呢?

1 个答案:

答案 0 :(得分:1)

您可以像使用此代码一样使用它...您需要使用两个while循环以便您可以使用mysql_data_seek()函数重置第一个循环并运行第二个循环正常...我对MySql的建议不是很高兴尝试使用MySqli。

if(isset($_POST['Generate']))
    {
        $emp = mysql_real_escape_string($_POST['employee']);
        $startD = $_POST['from'];
        $endD = $_POST['to'];
        $search = "SELECT leaveregister.*, users.first_name, users.last_name FROM leaveregister INNER JOIN users ON leaveregister.employee=users.id WHERE employee = '$emp' AND startDate >= '$startD' AND endDate <= '$endD'";
        $dosearch = mysql_query($search);
        if(mysql_num_rows($dosearch) > 0)
        {
            while($rows = mysql_fetch_assoc($dosearch)){
             $name = $rows['first_name'];
            }
            echo '<span>Employee : <strong>'.$name.'</strong></span>&nbsp;&nbsp;&nbsp;<span>From : <strong>'.$startD.'</strong></span>&nbsp;&nbsp;&nbsp;<span>To : <strong>'.$endD.'</strong></span><br><br><br>' ;
            echo '<table border="1" class="table table-bordered">';
            echo '<thead><th>Emp</th><th>Leave Type</th><th>Leave Duration</th><th>Start</th><th>End</th><th>Remarks</th></thead>';
            echo '<tbody>';
            mysql_data_seek($dosearch, 0);
            while($row = mysql_fetch_array($dosearch))
            {
                echo '<tr><td>'.$row['employee'].'</td><td>'.$row['leaveType'].'</td><td>'.$row['leaveDays'].'</td><td>'.$row['startDate'].'</td><td>'.$row['endDate'].'</td><td>'.$row['remarks'].'</td></tr>';
                }
            echo '</tbody></table>';
            }
            else
            {                       
            echo "Not Found!!";
            }
        }