So I have a table called "Flights" that stores the source and destination of a flight. Say you have A -> B and B -> C in a table, and you give the source as 'A' and destination as 'C', the query should return the path between these two A - > B, B -> C. I have tried this query:
$result = $mysqli -> query(
"SELECT *
FROM Flights a
JOIN Flights b
ON a.Destination = b.Source
AND '{$source}' = a.Source
AND '{$destination}' = b.Destination");
As a side note: I'm using mysqli in PHP. I have done this query in the mysql console and it returns everything I need. But if I ran this query in PHP on the example table I gave it would return B -> C only. Any ideas where I went wrong? I think it could have to do with the PHP string formatting.
<?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";
echo "$sql";
$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>";
}
$mysqli->close();
?>
That is my PHP code. I apologize because I know indenting is incorrect.