我试图让一个while循环在另一个循环中工作,我之前已经让它工作但又无法再管理它。
我试图计算航班查询中的总记录数,然后在小时记录中尝试添加时间。我已将所有查询单独工作,但当我将它们组合在一起时,它无法正常工作。这是我的代码,我可以更改哪些内容才能使其正常工作?
<?php
error_reporting (E_ALL ^ E_DEPRECATED);
// get results from database
$result = mysql_query("SELECT * FROM `phpvms_pilots` WHERE `confirmed` = '1' AND `retired` = '0' ORDER BY pilotid ASC;")
or die(mysql_error());
$resulttotalflights = mysql_query("SELECT COUNT(*) AS total_flights FROM `phpvms_pireps` WHERE `pilotid` = '$pid';")
or die(mysql_error());
$resulttotalhours = mysql_query("SELECT SEC_TO_TIME( SUM(TIME_TO_SEC( `flighttime_stamp` ) ) ) AS total_hours FROM phpvms_pireps WHERE pilotid = '$pid'")
or die(mysqli_error());
echo "<table width='100%' border='0' cellpadding='0' align='center'>";
echo "<tr>
<th align='center'>Pilot ID</th>
<th align='center'>Name</th>
<th align='center'>Rank</th>
<th align='center'>Total Flights</th>
<th align='center'>Total Hours</th>
<th align='center'>Vatsim ID</th>
<tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
//$pid = 'RFR' . $row['pilotid'];
$pid = $row['pilotid'];
$first = $row['firstname'];
$last = $row['lastname'];
$vid = $row['vatsimid'];
$rank = $row['rank'];
while($row_flights = mysql_fetch_array( $resulttotalflights )) {$flights = $row_flights['total_flights'];}
while($row_hours = mysql_fetch_array( $resulttotalhours )) {$hours = $row_hours['total_hours'];}
// echo out the contents of each row into a table
echo "<tr>";
//echo '<td>' . $pid . '</td>';
echo '<td><a href="' . $vaptams_profile . $pid . '">' . 'RFR' . $pid . '</a></td>';
echo '<td>' . $first . ' ' . $last . '</td>';
echo '<td>' . $rank . '</td>';
echo '<td>' . $flights . '</td>';
echo '<td>' . $hours . '</td>';
echo '<td><a href="' . $vatawere . $vid . '">' . $vid . '</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
由于 斯科特
答案 0 :(得分:0)
不是使用3个while
循环,每个循环都会出现一个会触发数据库的查询,为什么不只使用一个且只有这样的SQL查询:
SELECT
`phpvms_pilots`.*,
COUNT(`phpvms_pireps`.*) AS total_flights,
SEC_TO_TIME( SUM(TIME_TO_SEC( `flighttime_stamp` ) ) ) AS total_hours
FROM `phpvms_pilots`
LEFT JOIN `phpvms_pireps` ON `phpvms_pireps`.`pilotid` = `phpvms_pilots`.`pilotid`
WHERE
`phpvms_pilots`.`confirmed` = '1'
AND `phpvms_pilots`.`retired` = '0'
ORDER BY pilotid ASC;