2016-12-15 71 views
-1

我目前正在一個搜索系統,我想顯示所有有2列名稱的行,問題是,我的while循環只顯示1行,當我有2人在數據庫中。我一直在努力工作幾個小時,但我無法得到它。我得到的唯一結果是最後一個價格。雖然循環顯示只有1結果

<?php 
include_once("inc/head.php"); 
include_once("inc/db.php"); 
$leave = $_POST['departure']; 
$come = $_POST['destination']; 
$leavedate = $_POST['leavedate']; 
$comedate = $_POST['comedate']; 
$sql = "select * from flights where departure = '$leave' and destination ='$come'"; 
$result = $db->query($sql); 
$row = $result->fetchObject(); 
$price = $row->price; 
$mostflights = $row->departure; 
$mostcomings = $row->destination; 
?> 

<div> 
    <ul class="flights"> 
     <?php 
     while($row = $result->fetchObject()) 
     $price = $row->price; 
     $mostflights = $row->departure; 
     $mostcomings = $row->destination; 
     { ?> 

     <li> 
      <h3><?php echo $mostflights; ?></h3> 
      <h2><?php echo $mostcomings; ?></h2> 
      <h2><?php echo $price; ?></h2> 
     </li> 
     <?php 
     } 
    ?> 
    </ul> 
</div> 
+0

的結果是什麼,當你在你的DBMS嘗試手動查詢此塊? – Beginner

+0

從出發='$ leave'和destination ='$ come'的航班選擇*如果我用我從郵局獲得的名稱替換變量我得到一個成功的查詢 –

+0

爲什麼你使用'fetchObject()'兩次? –

回答

1

刪除在10日線

$row = $result->fetchObject(); 
$price = $row->price; 
$mostflights = $row->departure; 
$mostcomings = $row->destination; 
+0

這樣做給了相同的結果,我仍然只能看到最後一行的價格,如果我刪除價格變量,我可以看到最flyf值等。 –

+0

請在while循環之後將第一個打開的curl移動到'',像這樣'while($ row = $ result-> fetchObject()){' –

+0

這樣做,我也只是注意到我做錯了什麼。 –