2015-11-06 64 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循环内吗?用新代码更新了我的原始帖子。 –