PHP 返回的结果少于 mysql
PHP returning less results than mysql
我有一个 mysql 查询,当我在 mysql 控制台中 运行 returns 4 个结果时,这正是我需要的,但是当我尝试时在 PHP 我只得到 2 个结果。我的 PHP 看起来有什么问题吗?我已将查询回显到一个字符串中,将其粘贴到控制台中,它也提供了我需要的内容。当我回显返回的行数时,它是 2.
<?php
if(isset($_POST['source'])){
$source = $_POST['source'];
}
if(isset($_POST['destination'])){
$destination = $_POST['destination'];
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Airport Tracking";
$table ="Flights";
$flag = 0;
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['source']) && isset($_POST['destination'])){
$columns= $mysqli -> query("SHOW COLUMNS FROM $table");
$sql = "SELECT *
FROM Flights a
JOIN Flights b
ON a.Destination = b.Source
AND {$source} = a.Source
AND {$destination} = b.Destination";
$result = $mysqli -> query($sql);
if($result == FALSE){
echo "<br><b>Incorrect input</b>";
$flag = 1;
}
else if($result->num_rows == 0){
echo "<br><b>Returned no results</b>";
$flag = 1;
}
}
$array = array();
$i = 0;
if(isset($_POST['source']) && $flag == 0){
// Display results
echo "<table>";
echo "<thead><tr>";
while ($row = $columns -> fetch_array(MYSQLI_BOTH)) {
echo "<th>" .$row['Field']. "</th>";
$array[] = $row['Field'];
}
echo "</tr></thead>";
while ($row = $result -> fetch_array(MYSQLI_BOTH)) {
echo "<tr>";
while ($i < sizeof($array)) {
echo "<td>" .utf8_encode($row[$array[$i]]). "</td>";
$i++;
}
echo "</tr>";
$i = 0;
}
echo "</table>";
您需要在带有空格的字段名称周围加上反引号。您需要像这样更改这些行:
AND `{$source}` = a.Source
AND `{$destination}` = b.Destination";
从 self-join 返回列时不要使用 SELECT *
。 a
和 b
中的列相同,但您需要为它们指定不同的名称。所以使用类似的东西:
SELECT a.source AS start,
a.flightno AS flight1,
a.destination AS connection,
b.flightno AS flight2,
b.destination AS destination
然后在显示 table 的代码中执行:
while ($row = $result->fetch_assoc()) {
foreach ($row as $col) {
echo "<td>" . utf8_encode($col) . "</td>";
}
}
您还需要更改显示列标题的代码,以使用与您选择的这些列相对应的标题,而不是从 SHOW COLUMNS
获取它们(因为那样只会得到一组列名)。
我有一个 mysql 查询,当我在 mysql 控制台中 运行 returns 4 个结果时,这正是我需要的,但是当我尝试时在 PHP 我只得到 2 个结果。我的 PHP 看起来有什么问题吗?我已将查询回显到一个字符串中,将其粘贴到控制台中,它也提供了我需要的内容。当我回显返回的行数时,它是 2.
<?php
if(isset($_POST['source'])){
$source = $_POST['source'];
}
if(isset($_POST['destination'])){
$destination = $_POST['destination'];
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Airport Tracking";
$table ="Flights";
$flag = 0;
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['source']) && isset($_POST['destination'])){
$columns= $mysqli -> query("SHOW COLUMNS FROM $table");
$sql = "SELECT *
FROM Flights a
JOIN Flights b
ON a.Destination = b.Source
AND {$source} = a.Source
AND {$destination} = b.Destination";
$result = $mysqli -> query($sql);
if($result == FALSE){
echo "<br><b>Incorrect input</b>";
$flag = 1;
}
else if($result->num_rows == 0){
echo "<br><b>Returned no results</b>";
$flag = 1;
}
}
$array = array();
$i = 0;
if(isset($_POST['source']) && $flag == 0){
// Display results
echo "<table>";
echo "<thead><tr>";
while ($row = $columns -> fetch_array(MYSQLI_BOTH)) {
echo "<th>" .$row['Field']. "</th>";
$array[] = $row['Field'];
}
echo "</tr></thead>";
while ($row = $result -> fetch_array(MYSQLI_BOTH)) {
echo "<tr>";
while ($i < sizeof($array)) {
echo "<td>" .utf8_encode($row[$array[$i]]). "</td>";
$i++;
}
echo "</tr>";
$i = 0;
}
echo "</table>";
您需要在带有空格的字段名称周围加上反引号。您需要像这样更改这些行:
AND `{$source}` = a.Source
AND `{$destination}` = b.Destination";
从 self-join 返回列时不要使用 SELECT *
。 a
和 b
中的列相同,但您需要为它们指定不同的名称。所以使用类似的东西:
SELECT a.source AS start,
a.flightno AS flight1,
a.destination AS connection,
b.flightno AS flight2,
b.destination AS destination
然后在显示 table 的代码中执行:
while ($row = $result->fetch_assoc()) {
foreach ($row as $col) {
echo "<td>" . utf8_encode($col) . "</td>";
}
}
您还需要更改显示列标题的代码,以使用与您选择的这些列相对应的标题,而不是从 SHOW COLUMNS
获取它们(因为那样只会得到一组列名)。