2015-11-06 49 views
0

我能夠從數據庫中檢索並顯示一行數據,但是我想要檢索多行數據。我所擁有的代碼已經搞砸了,但我認爲我只是在正確的領域進行這項工作。在PhP中顯示多行

我的代碼:

$query = "SELECT * FROM hotel"; 
$results = $conn->query($query); 
    while($hotel = $results->fetch()) { echo "<br> id: ". $hotel['hotel_id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " . 
$hotel['postcode']. " - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . <img src='". $hotel['image']. "'>""; 
    } 
$conn=NULL; 
?> 
+0

你如何當你執行你的SQL查詢出來的代碼的結果呢? – Snoozer

回答

0

我看到你正在使用PDO。你可以簡單地遍歷這樣的結果。

while($results_disp = $results->fetch(PDO::FETCH_OBJ)) 
{ 
//access rows here in this manner 
echo "Hotel Id :- " . $results_disp->id; 
} 
+0

它給了我這些: –

+0

啊!我的錯。我更新了我的代碼。 – Akshay

+0

仍然給我這2個錯誤; /我已編輯以前的帖子 –

0
while($hotel = $results->fetch()) { 
    echo "<br> id: ". $hotel['id']. " - Name: ". $hotel['name']. " - Address: " . $hotel['address'] . " - Postcode: " . $hotel['postcode']. 
    " - Town: " . $hotel['town']. " - Description: ". $hotel['description']. " - rating: ". $hotel['rating']. " - image: " . $hotel['image']; 
} 

這種替換while循環。

+0

正確的等待emm哪些「$」我需要刪除? –

+0

你需要編輯以下內容: $ hotel [$ id] = $ hotel [id],$ hotel [$ name] = $ hotel [name],$ hotel [address]等 –

+0

要麼我在做錯誤或它不起作用:( –

1

有幾件事你的代碼錯誤:

$query = "SELECT * FROM hotel"; 
$results = $conn->query($query); 
$hotel = $results->fetch(); 

在這一點上,加載了第一條記錄爲$hotel。如果你想遍歷記錄集,你不需要這樣做。

$hotel[$id] = $hotel['hotel_id']; 
$hotel[$name] = $hotel['name']; 
$hotel[$address] = $hotel['address']; 
$hotel[$postcode] = $hotel['postcode']; 
$hotel[$town] = $hotel['town']; 
$hotel[$description] = $hotel['description']; 
$hotel[$rating] = $hotel['rating']; 
$hotel[$image] = $hotel['image']; 

這些行有些多餘。您已經獲得了$hotel中該行的詳細信息,並且您正在使用尚未定義的數組鍵添加額外的條目。

/* 
$id = $hotel['hotel_id']; 
$name = $hotel['name']; 
$address = $hotel['address']; 
$postcode = $hotel['postcode']; 
$town = $hotel['town']; 
$description = $hotel['description']; 
$rating = $hotel['rating']; 
$image = $hotel['image']; 
*/ 
while($hotel = $results->fetch()) { 
    echo "<br> id: ". $hotel[$id] = $hotel[id]. " - Name: ". $hotel[$name]. " - Address: "  . $hotel[$address] . " - Postcode: " . $hotel[$postcode]. 
    " - Town: " . $hotel[$town]. " - Description: ". $hotel[$description]. " - rating: ". $hotel[$rating]. " - image: " . $hotel[$image]; 
} 

在你的循環中,你試圖錯誤地訪問數組鍵。 $hotel[$id]正在使用$id作爲關鍵字,並且該變量不存在。 $hotel[id]正在使用不帶引號的密鑰,PHP會猜測它是$hotel['id'],但會抱怨。所以你可以直接引用它來避免警告。

所以,你可以簡化你的代碼到:

$query = "SELECT * FROM hotel"; 
$results = $conn->query($query); 

while($hotel = $results->fetch()) { 
    echo "<br> id: ". $hotel['id'] . " - Image: <img src='". $hotel['image']. "'>" .... 
} 
+0

好吧看起來不錯,不顯示圖像,我將如何解決這個問題?並且它只顯示一行id:2並且忽略id:1的行,並且在我的數據庫中有這個http://screenshot.sh/odX3fHeLnBJ7O –

+0

如果你想要一個圖像,你必須添加HTML標記並將其包裝在''標記中。上面的代碼將顯示數據庫表中的所有內容,並且不會跳過任何記錄。您是否使用了確切的代碼,刪除了所有現有的代碼? – andrewsi

+0

我相信你不能在php標籤內做到這一點,但它需要在while while循環內嗎?用新代碼更新了我的原始帖子。 –