call the function twice?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    call the function twice?

    I'm having problem with this code i can't describe the problem, that is my code:

    [PHP]<?php
    $announce = mysql_query("SE LECT * FROM announce LIMIT 4");
    while($key = mysql_fetch_ass oc($announce)){
    echo '<td style="border:# 999999 solid 1px;"><center>' .$key['Title'].'</center></td>';
    }
    ?>
    </tr>
    <tr>
    <?php
    while($key = mysql_fetch_ass oc($announce)){
    for($i=1;$i<=3; $i++){
    $PicArray = 'Pic'.$i;
    if($key[$PicArray]!=''){
    $PicType = explode(',',$ke y[$PicArray]);
    $tum = $PicType[1];
    if(is_file($tum )){
    echo '<td style="border:# 999999 solid 1px;"><center>< img src="'.$tum.'"/></center></td>';
    break;
    }
    }
    }
    }
    ?>[/PHP]

    in this code to solve it i have to call this function again in the second row why?

    [PHP]$announce = mysql_query("SE LECT * FROM announce LIMIT 4");[/PHP]
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    The result returned by the mysql_query() function keeps an internal row pointer, which is incremented whenever you call a mysql_fetch_*() function on it.

    So after the first loop has completed, the internal pointer has already reached the end of the result set, so any mysql_fetch_*() call will fail on it.

    Try using the mysql_data_seek function on it before the second loop. Like:
    [code=php]
    $result = mysql_query("SE LECT cols FROM tbl");
    while($row = mysql_fetch_ass oc($result)) {#...}

    mysql_data_seek ($result, 1);
    while($row = mysql_fetch_ass oc($result)) {#...}
    [/code]

    Comment

    Working...