Jump to content

Calling mysql_fetch_array twice on the same $result...Legal?


kvishnu_13

Recommended Posts

This was what I was trying to do:

 

echo '<table align = "center"
               border="0" cellpadding="5" cellspacing="0">
               <tbody>
               <tr>
               <td align = "left"><b>Company Name</b></td>
               <td align = "left"><b>Direct Stocks Available</b></td>
               <td align = "left"><b>Per Stock Price</b></td>
               <td align = "left"><b>Company Rating</b></td>
               <td align = "left"><b>Buy</b></td>
               </tr>';
     $query = "SELECT * FROM Companies WHERE comp_status = '0'";
     $result = @mysql_query($query);
     if($result) {
          while($row = mysql_fetch_array($result, MYSQL_ASSOC))
          {
               $query2 = "SELECT pl_rating FROM Players WHERE pl_id = '$row[pl_id]'";
               $result2 = mysql_query($query2);
          }
     }
          while($row2 = mysql_fetch_array($result, MYSQL_ASSOC) && $row3 = mysql_fetch_array($result2, MYSQL_ASSOC))
          {
                $stocks_num = floor($row2['comp_totValue']/$row2['comp_value']);
                echo '    <tr>
                    <td align = "left"><a href = "pl_info.php?id=' . $row2['pl_id'] . '"><b>' . $row2['comp_name'] . '</b></a></td>
                    <td align = "left">' . $stocks_num . '</td>
                    <td align = "left">' . $row2['comp_value'] . '</td>
                    <td align = "left">' . $row3['pl_rating'] . '</td>
                    <td align = "left"><a href = "buyD_comp.php?id=' . $row2['comp_id'] . '">Buy</a></td>
                    </tr>';
          }
     }
     echo '</tbody>
     </table>';

The problem is, the code is not running through the second while loop, which makes me question whether I can call msql_fetch_result() twice on the same $result.

Any thoughts on the error here.

No you can't. Your first while loop will loop through all the rows in the result resource until there are none left, hence your second while loop will not find any more rows to loop through.

Yes, I just looked that up, and apparently if u use mysql_data_seek($result), the result will go through the array from the beginning again. Weird thing is...even after I use mysql_data_seek, the code doesn't work. I think the problem is with the syntax of the second while loop itself. Is the syntax right? I am trying to look up stuff as I am posting these messages. Will post a reply myself if I find a solution. Thanks a lot for the reply.

It looks to me like you are getting confused with your code structure there. I think what you want to achieve might be accomplished with a single query with a simple join on the two tables, and a single while loop. Currently you are going through the first while loop and not actually doing anything with each row, so in the second loop the if statement will always be using the final result from the first one.

It looks to me like you are getting confused with your code structure there. I think what you want to achieve might be accomplished with a single query with a simple join on the two tables, and a single while loop. Currently you are going through the first while loop and not actually doing anything with each row, so in the second loop the if statement will always be using the final result from the first one.

 

That works..that works. Thanks a lot man. I really appreciate ur help.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.