0

Hi am trying to go through a list of xml results and I am taking the id from these results and performing a query in my database to get more information. I have placed this into a foreach loop and then I have my while loop getting the mysql results. Everything works until I do the while loop. My page goes blank and I get no errors.. ANY help would be so appreciated! Here is my code:

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; ?>

Just as a note, I have tried to get num_rows and I do get the correct result. SO the query is executing, but something is happening in the while loop.

0

3 Answers 3

2

Your while loop is bad. Take out the $ in front of the function.

while($r=mysql_fetch_array($getImages)):
Sign up to request clarification or add additional context in comments.

Comments

1

Your main problem is that you put a $ in front of a method call ($mysql_fetch_array()). It should just be mysql_fetch_array().

An important issue that you shouldn't overlook is that you have a static query being called within a loop. Perform the query outside of your loop, since the results will never change. You can then store the results in an array and iterate through the array within your loop. This will significantly improve your code's performance.

<?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; ?>

2 Comments

I am an idiot... this is such an obvious mistake. Thank you for taking the time to answer.
No worries. Out of curiosity, where do you use img? I don't see it called in your code anywhere.
1
while($r=$mysql_fetch_array($getImages)):
    $img = $r['Caption'];
?>

You have a $ before the mysql_fetch_array(), removing that should fixed your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.