在php中使用foreachloop中的while循环执行mysql_查询
您好,我正在查看xml结果列表,我从这些结果中获取id,并在数据库中执行查询以获取更多信息。我已经将其放入foreach循环中,然后使用while循环获得mysql结果。在我执行while循环之前,一切都正常。我的页面变为空白,没有错误。。任何帮助都将不胜感激! 这是我的密码:在php中使用foreachloop中的while循环执行mysql_查询,php,mysql,xml,foreach,while-loop,Php,Mysql,Xml,Foreach,While Loop,您好,我正在查看xml结果列表,我从这些结果中获取id,并在数据库中执行查询以获取更多信息。我已经将其放入foreach循环中,然后使用while循环获得mysql结果。在我执行while循环之前,一切都正常。我的页面变为空白,没有错误。。任何帮助都将不胜感激! 这是我的密码: foreach($data->HotelList->HotelSummary as $info): $hotelId = $info->hotelId; $title=$info->
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; ?>
foreach($data->HotelList->HotelSummary as$info):
$hotelId=$info->hotelId;
$title=$info->name;
?>
你的while循环不好。取出函数前面的$
while($r=mysql_fetch_array($getImages)):
你的while循环不好。取出函数前面的$
while($r=mysql_fetch_array($getImages)):
在mysql\u fetch\u array()
之前有一个$
,删除它应该可以解决您的问题
在mysql\u fetch\u array()
之前有一个$
,删除它应该可以解决您的问题 您的主要问题是在方法调用前面放置了一个$
($mysql\u fetch\u array()
)。它应该是mysql\u fetch\u 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; ?>
您的主要问题是在方法调用前面放置了一个$
($mysql\u fetch\u array()
)。它应该是mysql\u fetch\u 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; ?>
我是个白痴。。。这是一个明显的错误。谢谢你花时间回答。不用担心。出于好奇,您在哪里使用img
?我在你的代码里没有看到它被调用。我是个白痴。。。这是一个明显的错误。谢谢你花时间回答。不用担心。出于好奇,您在哪里使用img
?我看你的代码中没有调用它。