已解决未定义错误,但现在只显示1条记录,而不是预期3条用于查询

时间:2014-05-10 02:19:07

标签: php

解决未定义的偏移错误后,错误消息不再存在,但现在显示的结果只是一条记录而不是预期的记录数,例如3。

$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
    $time = $row['vluchttijd'];
    if (!function_exists('timeInWords')) {
    function timeInWords($time) {
        list($hours, $minutes) = explode(':', $time);
        return (int)$hours .  " hrs " . (int)$minutes . " min";
   }    $result = mysql_query($sql);

{

我可以从$ time中删除所有内容然后返回所有预期记录。

按要求提供所有代码。我应该预先假定我对PHP世界还很新,所以请放轻松我:

<?php

include('../datalogin.php');    // include your code to connect to DB.


mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
 die("Invalid ID specified.");
} 
$rID = (int)$_GET['id'];

$sql = "a bunch of SQL code removed to save space

 WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";

$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$time = $row['vluchttijd'];
if (!function_exists('timeInWords')) {
function timeInWords($time) {
    list($hours, $minutes) = explode(':', $time);
    return (int)$hours .  " hrs " . (int)$minutes . " min";
}   $result = mysql_query($sql);

{
echo "<table border='0' width='640'>";
echo "<tbody>";
echo "<tr>";
echo "<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2'])). "  
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_vertrek'>(D)  
".htmlspecialchars($row['luchthavennaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td width='18%'><strong>Duration:</strong></td>";
echo "<td width='41%'>".timeInWords($time)."</td>";
echo "<td rowspan='4' width='41%' align='center' valign='middle'><img    
src='../logos/".$row['logo']."'</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Equipment:</strong></td>";
echo "<td>".$row['toestel']."&nbsp;".$row['erlr']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Class:</strong></td>";
echo "<td>".$row['reisklass']."</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Miles:</strong></td>";
echo "<td>".$row['afstand']." miles</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='12'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><span class='c_aankomst'>(A) 
".htmlspecialchars($row['aankomstnaam'])."</span></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3' height='1' bgcolor='#585858'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'></td>";
echo "</tr>";
}
}
}
echo "</table>";
?>

1 个答案:

答案 0 :(得分:0)

这应该这样做,你再次使用相同的查询调用mysql_query,这样就可以一直给你相同的结果。

我在循环时清理了你的开始,所以它只从结果对象中获取数据并打印出来。你的时间函数不需要在循环内部,所以我把它移到了外面。

我还从一些回声中清除了循环

<?php

include('../datalogin.php');    // include your code to connect to DB.

function timeInWords($time) {
    list($hours, $minutes) = explode(':', $time);
    return (int)$hours .  " hrs " . (int)$minutes . " min";
}


mysql_query("SET CHARACTER SET 'utf8';");//GET and POST
mysql_query("SET NAMES 'utf8';");//POST
/* Get data. */
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
 die("Invalid ID specified.");
} 
$rID = (int)$_GET['id'];

$sql = "a bunch of SQL code removed to save space

 WHERE vg.reisID = '$rID'
ORDER BY vg.vertrekdatum2 ASC";

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result))
{
$time = $row['vluchttijd'];    

echo "<table border='0' width='640'>
<tbody>
<tr>
<td colspan='3'><strong>" .date('d-M-Y H:i', strtotime($row['vertrekdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_vertrek'>(D)  
".htmlspecialchars($row['luchthavennaam'])."</span></td>
</tr>
<tr>
<td width='18%'><strong>Duration:</strong></td>
<td width='41%'>".timeInWords($time)."</td>
<td rowspan='4' width='41%' align='center' valign='middle'><img    
src='../logos/".$row['logo']."'</td>
</tr>
<tr>
<td><strong>Equipment:</strong></td>
<td>".$row['toestel']."&nbsp;".$row['erlr']."</td>
</tr>
<tr>
<td><strong>Class:</strong></td>
<td>".$row['reisklass']."</td>
</tr>
<tr>
<td><strong>Miles:</strong></td>
<td>".$row['afstand']." miles</td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3' height='12'></td>
</tr>
<tr>
<td colspan='3'><strong>".date('d-M-Y H:i', strtotime($row['aankomstdatum2']))."
</strong></td>
</tr>
<tr>
<td colspan='3'><span class='c_aankomst'>(A) 
".htmlspecialchars($row['aankomstnaam'])."</span></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3' height='1' bgcolor='#585858'></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>
<tr>
<td colspan='3'></td>
</tr>";


}
echo "</table>";
?>