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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.