kvishnu_13 Posted June 16, 2007 Share Posted June 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55856-calling-mysql_fetch_array-twice-on-the-same-resultlegal/ Share on other sites More sharing options...
king arthur Posted June 16, 2007 Share Posted June 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55856-calling-mysql_fetch_array-twice-on-the-same-resultlegal/#findComment-275957 Share on other sites More sharing options...
kvishnu_13 Posted June 16, 2007 Author Share Posted June 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55856-calling-mysql_fetch_array-twice-on-the-same-resultlegal/#findComment-275960 Share on other sites More sharing options...
sasa Posted June 16, 2007 Share Posted June 16, 2007 Use mysql_data_seek($result,0) Quote Link to comment https://forums.phpfreaks.com/topic/55856-calling-mysql_fetch_array-twice-on-the-same-resultlegal/#findComment-275981 Share on other sites More sharing options...
king arthur Posted June 16, 2007 Share Posted June 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55856-calling-mysql_fetch_array-twice-on-the-same-resultlegal/#findComment-275988 Share on other sites More sharing options...
kvishnu_13 Posted June 16, 2007 Author Share Posted June 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55856-calling-mysql_fetch_array-twice-on-the-same-resultlegal/#findComment-276030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.