您好,我正在尝试浏览xml结果列表,并从这些结果中提取id并在我的数据库中执行查询以获取更多信息。我将其放入foreach循环中,然后让while循环获取mysql结果。在我执行while循环之前,一切都会正常工作。我的页面变成空白,没有任何错误..任何帮助都将不胜感激!下面是我的代码:
foreach($data->HotelList->HotelSummary as $info):
$hotelId = $info->hotelId;
$title=$info->name;
?>
<!---------------------------------------------------------------------->
<!-----------------Listed hotel results div----------------------------->
<!---------------------------------------------------------------------->
<div class="listResults">
<div class="hotelListing">
<div class="hotelThumb">
<?php
//----------Getting thumb url from database by HotelId-------
$getImages = mysql_query("SELECT Caption FROM HotelImages WHERE ID = '4110'") or die(mysql_error());
while($r=$mysql_fetch_array($getImages)):
$img = $r['Caption'];
?>
<img src="" width="200" height="180" alt="test image" class="thumb" />
<?php endwhile; ?></div>
<?php endforeach; ?>顺便说一句,我已经尝试过获取num_rows,但我确实得到了正确的结果。所以查询正在执行,但是while循环中发生了一些事情。
发布于 2012-09-12 22:59:05
您的主要问题是在方法调用($mysql_fetch_array())之前放置了一个$。它应该只是mysql_fetch_array()。
您不应该忽略的一个重要问题是,您有一个在循环中调用的静态查询。在循环之外执行查询,因为结果永远不会改变。然后,您可以将结果存储在数组中,并在循环中迭代该数组。这将显著提高代码的性能。
<?php
//----------Getting thumb url from database by HotelId-------
$getImages = mysql_query("SELECT Caption FROM HotelImages WHERE ID = '4110'") or die(mysql_error());
$images = array();
while($img = mysql_fetch_array($getImages)) {
$images[] = $img['Caption'];
}
foreach($data->HotelList->HotelSummary as $info):
$hotelId = $info->hotelId;
$title=$info->name;
?>
<!---------------------------------------------------------------------->
<!-----------------Listed hotel results div----------------------------->
<!---------------------------------------------------------------------->
<div class="listResults">
<div class="hotelListing">
<div class="hotelThumb">
<?php
foreach($images as $img) :
?>
<img src="" width="200" height="180" alt="test image" class="thumb" />
<?php endforeach; ?></div>
<?php endforeach; ?>发布于 2012-09-12 22:50:32
你的while循环不好。取出函数前面的$。
while($r=mysql_fetch_array($getImages)):发布于 2012-09-12 22:50:37
while($r=$mysql_fetch_array($getImages)):
$img = $r['Caption'];
?>您在mysql_fetch_array()之前有一个$,删除它可以解决您的问题。
https://stackoverflow.com/questions/12390842
复制相似问题