下面的代码适用于一个if语句,但如果并且它正在显示'航班'第一个查询中的表但在另一个条件之后没有显示名为' 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);
?>
答案 0 :(得分:2)
将表名括在反引号(`)中而不是单引号('):
SELECT * FROM `isb to murree`
为避免将来出现此问题和其他问题,请始终在反引号中包含模式,表和列名称,以区别于数据和MySQL关键字。考虑使用没有空格的表名,以便于处理。
作为Fred noted,您的代码使用了两个数据库API:mysql_*
和mysqli_*
。一般来说,两者混合不好。考虑升级所有代码以使用mysqli_*
或PDO。有关mysql_*
弃用说明,请参阅Deprecated features in PHP 5.5.x和Why are PHP's mysql_ functions deprecated?。
答案 1 :(得分:0)
您应该将数据库连接变量移到代码的顶部(因此它们在if语句之外)
$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';