我正在将旧的PHP从mysql转换为mysqli,并在转换过程中遇到此错误。代码工作时没有在mysql下抛出错误但由于某种原因我得到了#102;未定义变量:在第102行'中的/xx/xx/xx/xx/xx/xx/vluchtbijtoestel.php中的prevcat。现在错了。我已经尝试将$ prevcat = $ cat添加到变量列表中,但这会破坏代码。
我的代码:
<?php
define('DB_SERVER', "xxx");
define('DB_USER', "xxx");
define('DB_PASSWORD', "xxx");
define('DB_TABLE', "xxx");
// Define your colors for the alternating rows
$color1 = "#F0F8FF";
$color2 = "#FFFFFF";
$row_count = 0;
// The procedural way
$mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE);
$mysqli->set_charset("utf8");
$mysqli->set_charset("utf8");
$mysqli->query("SET NAMES 'utf8'");
if (mysqli_connect_errno($mysqli)) {
trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR);
}
$query = "
SELECT vg.gegevenID, lvm.luchtvaartmaatschappij, lvm.sm_logo, vg.vertrekdatum2, vg.vertrekluchthaven, vg.aankomstluchthaven, vg.toestel, vg.inschrijvingnmr, vlg.vliegtuignaam, vg.vluchtnmr, t.toestel AS toestelnaam, lh.luchthavencode, lh.luchthavennaam, lh.countryflag, lh2.luchthavencode AS aankomstluchthavencode, lh2.countryflag AS countryflagaankomst, vlg.erlr, vlg.firstflight, DATEDIFF(vlg.firstflight,vg.vertrekdatum2) Age1, lh2.luchthavennaam AS aankomstnaam, CONCAT(t.toestel,' ', vlg.erlr) as toestelmeterlr
FROM (tbl_vliegtuiggegevens vlg
INNER JOIN (tbl_luchtvaartmaatschappij lvm INNER JOIN tbl_vluchtgegevens vg ON lvm.luchtvaartmaatschappijID = vg.luchtvaartmaatschappij) ON (vlg.inschrijvingnmr = vg.inschrijvingnmr) AND (vlg.lvmID = lvm.luchtvaartmaatschappijID)) INNER JOIN tbl_toestel t ON vg.toestel = t.toestelID
LEFT JOIN tbl_luchthaven lh
ON vg.vertrekluchthaven = lh.luchthavenID
LEFT JOIN tbl_luchthaven lh2
ON vg.aankomstluchthaven = lh2.luchthavenID
GROUP BY lvm.luchtvaartmaatschappij, toestelmeterlr, vg.inschrijvingnmr, vg.vertrekdatum2
ORDER BY lvm.luchtvaartmaatschappij, toestelmeterlr, vg.vertrekdatum2 DESC, vg.inschrijvingnmr; ";
$result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);
echo "
<table width='100%' border='0'>
<tr>
<td colspan='3'><strong>Luchtvaartmaatschappij</strong></td>
<td width='17%'> </td>
<td width='12%'> </td>
<td width='19%'> </td>
<td width='28%'> </td>
</tr>
<tr>
<td width='2%'> </td>
<td colspan='3'><strong>Toestel</strong></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td width='3%'> </td>
<td width='19%'><strong>vertrekdatum</strong></td>
<td><strong>vliegroute</strong></td>
<td><strong>registratie</strong></td>
<td><strong>vliegtuignaam</strong></td>
<td><strong>vliegtuig leeftijd op reisdatum</strong></td>
</tr>
</table>
";
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
$date1 = date_create($row['firstflight']);
$date2 = date_create($row['vertrekdatum2']);
$interval = date_diff($date1, $date2);
$cat = $row['luchtvaartmaatschappij'];
$subcat = $row['toestelmeterlr'];
$item = $row['inschrijvingnmr'];
if($cat != $prevcat){
echo " <table width='100%' border='0'>";
echo " <tr>";
echo " <td><br /><hr></td>";
echo " </tr>";
echo " <tr>";
echo " <td><strong><span class='style4'><img src='http://globe-trekking.com/vg/img/logos/sm/".$row['sm_logo']."'> " .$cat. "</strong></span> <br /></td>";
echo " </tr>";
echo "</table>";
echo '<span style="color: #0A0094; font-size: 16px;"> '. $subcat.'</span><br />';//if the category has changed, we also want to show the new subcat
}elseif($subcat != $prevsubcat){
echo '<br /><span style="color: #0A0094; font-size: 16px;"> ' .$subcat.'</span><br />';
}
echo " <table width='1093' border='0' cellspacing='0'>";
echo " <tr>";
echo " <td width='75'> </td>";
echo " <td width='200' bgcolor='$row_color'>" .date("d-M-Y H:i", strtotime($row['vertrekdatum2']))."</td>";
echo " <td width='70' bgcolor='$row_color'><img src='http://globe-trekking.com/vg/img/flags/".$row['countryflag']."'> <abbr title=\"".htmlspecialchars($row['luchthavennaam'])."\">".$row['luchthavencode']."</abbr></td>";
echo " <td width='70' bgcolor='$row_color'> naar </td>";
echo " <td width='130' bgcolor='$row_color'><img src='http://globe-trekking.com/vg/img/flags/".$row['countryflagaankomst']."'> <abbr title=\"".htmlspecialchars($row['aankomstnaam'])."\">".$row['aankomstluchthavencode']."</abbr></td>";
echo " <td width='150' bgcolor='$row_color'>".$row['inschrijvingnmr']."</td>";
echo " <td width='250' bgcolor='$row_color'>".$row['vliegtuignaam'] ."</td>";
echo " <td width='250' bgcolor='$row_color'>" . $interval->y . " jaar en " . $interval->m." maanden </td>";
echo " </tr>";
echo "</table>";
$prevcat = $cat;
$prevsubcat = $subcat;
// Add 1 to the row count
$row_count++;
}
}
mysqli_close($mysqli);
?>
答案 0 :(得分:0)
把你的代码..你在if条件中使用$ precat和$ prevsubcat,然后再声明并定义它。
$prevcat = $cat;
$prevsubcat = $subcat;
后
$cat = $row['luchtvaartmaatschappij'];
$subcat = $row['toestelmeterlr'];
像这样
$cat = $row['luchtvaartmaatschappij'];
$subcat = $row['toestelmeterlr'];
$prevcat = $cat;
$prevsubcat = $subcat;
答案 1 :(得分:0)
放
$ prevcat =''; $ prevsubcat ='';
Juste在“While”表达之前
此致