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

时间:2018-11-14 20:17:33

标签: php mysql 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();

1 个答案:

答案 0 :(得分:-1)

这可能是您的特定问题。

$result = mysqli_multi_query($query);

这里是PHP Doc

您需要建立连接。

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

就像评论中提到的其他人一样,我将重构此代码。