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
3,0683 gold badges33 silver badges49 bronze badges
asked Aug 26, 2014 at 19:40

2 Answers 2

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.

answered Aug 26, 2014 at 19:45
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Yes, indeed. Just an added note, OP is mixing APIs with mysqli_close($con);
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';
answered Aug 26, 2014 at 19:56

5 Comments

yes now its working for the three elseif conditions but not for the 4th one..syntax is same . what should be the problem?? @billy
Double check your if statements.
And check your inputs, murree is lowercase where Lahore is title case.
yess they are okay.. elseif ( $from =='Lahore'){ if($To == 'Islamabad'){ @billy
in this part its diverted now from Lahore to Islamabad... @billy

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.