0

The code below is working for one if statement but not giving results for another else if and it is showing the 'flights' table in first query but after another condition not display the other table named 'isb to muree'

<?php

$from =  isset($_POST['from'])?$_POST['from']:'';
 $To = isset($_POST['To'])?$_POST['To']:'';

 if( $from =='Islamabad'){
     if($To == 'Lahore'){


$db_host = 'localhost';
$db_user = 'root';

$database = 'homedb';
//$table = 'flights';

if (!mysql_connect($db_host, $db_user))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");
$result = mysql_query("SELECT * FROM flights");
if (!$result) {
    die("Query to show fields from table failed");
}



$fields_num = mysql_num_fields($result);

echo "<h1>Table: 'flights'</h1>";
echo "<table border='1'><tr>";


while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
}

else if( $from =='Islamabad'){
     if($To == 'murree'){
if (!mysql_connect($db_host, $db_user))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

$result = mysql_query("SELECT * FROM 'isb to murree'");
if (!$result) {
    die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);

echo "<h1>Table: 'isb to murree'</h1>";
echo "<table border='1'><tr>";


while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";

}
}
}
}


mysqli_close($con);
?>
kdmurray
  • 2,988
  • 3
  • 32
  • 47
user3909877
  • 99
  • 1
  • 2
  • 10

2 Answers2

2

Enclose the table name in backticks (`) instead of single quotes ('):

SELECT * FROM `isb to murree`

To avoid this and other problems in the future, always enclose schema, table, and column names in backticks to distinquish them from data and MySQL keywords. Consider using table names without spaces for easier handling.

As Fred noted, your code is using two database APIs: mysql_* and mysqli_* . In general, the two do not mix well. Consider upgrading all of your code to use mysqli_* or PDO. See Deprecated features in PHP 5.5.x and Why are PHP's mysql_ functions deprecated? for mysql_* deprecation notes.

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
0

You should move the database connection variables to the top of your code (so they are outside of the if statement)

$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';
Billy
  • 165
  • 6