使用php和mysqli获取结果时出现问题



运行以下php脚本时遇到问题:

<?php
$link = mysqli_connect("localhost", "root", "*******", "adsb");
/* check connection */
if ($conn->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$query = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id='2414';";
$query .= "SELECT aircrafts.id, plane.reg, plane.hex, plane.typ, plane.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `plane` ON aircrafts.hex=plane.hex WHERE aircrafts.id='2414'";
$result = mysqli_multi_query($query);
/* execute multi query */
if ($result->num_rows > 0) {
echo "<h1>INFO ABOUT FLIGHT RECORD &nbsp;" . $id . "</h1>";
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
/* close connection */
mysqli_close($link);

当我在phpmyadmin中运行2个查询时,我得到1个结果。(当我在我的网站上运行1个查询时,我也会得到一个结果,请参阅本文底部的代码(但当我在我的网站上运行它时,它显示"0个结果"。

所以。。。$result部分中必须有一个犯规。

谁能帮忙?:-(

<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "adsb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT aircrafts.id, heli.reg, heli.hex, heli.typ, heli.opp, aircrafts.flight, aircrafts.altitude, aircrafts.lat, aircrafts.lon, aircrafts.squawk, aircrafts.message_date FROM `aircrafts` JOIN `heli` ON aircrafts.hex=heli.hex WHERE aircrafts.id=2414";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><th>Registratie</th><th>ICAO24</th><th>Type</th><th>Operator</th><th>Callsign</th><th>Squawk</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td><a href='aircraft.php?hex=" . $row["hex"] . "'>" . $row["reg"] . "</a></td><td>" . $row["hex"] . "</td><td><img src='/database/SilhouttesLogos/" . $row["typ"] . ".bmp' /></td><td><img src='/database/OperatorFlags/" . $row["opp"] . ".bmp' /></td><td>" . $row["flight"] . "</td><td>" . $row["squawk"] . "</td><td>" . $row["message_date"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();

这可能是您的特定问题。

$result = mysqli_multi_query($query);

这是PHP Doc

您需要建立连接。

$result = mysqli_multi_query($link, $query);

和评论中提到的其他代码一样,我会重构这段代码。

最新更新