beefwellington Posted April 27, 2009 Share Posted April 27, 2009 Background Info: When a user visits this webpage, they search for company assets based on a number of things. In this example, it's based on the asset's tag. When a user searches by the asset tag, it displays a table and goes through a while loop to display each row of data for that asset. Since only 1 asset tag is assigned to 1 asset, only 1 row should be displayed. The WORKING while loop displays 1 row of data for 1 tag. But the BROKEN code will display an infinite amount of row's of the same data until the 30 second query limit is exceeded. Could someone shed some light as to why this is happening?WORKING LOOP $sql_GetAssetInfo = mssql_query("SELECT * FROM tbl_AssetLog WHERE AssetTag =" . $Tag); while($row = mssql_fetch_array($sql_GetAssetInfo)) {//loop code} BROKEN LOOP while($row = mssql_fetch_array(mssql_query("SELECT * FROM tbl_AssetLog WHERE AssetTag =" . $Tag))) {//loop code} Link to comment Share on other sites More sharing options...
jeffman Posted April 27, 2009 Share Posted April 27, 2009 mssql_query() returns a unique resource associated with the entire result set.In your broken version, you're sending the query with every iteration, and it's returning a unique result set every time. So every time you call mssql_fetch_array(), it returns the first result. So you're getting the first result, over and over, and it never ends.You only need to send the query one time to get everything you want.As for the working version, if you absolutely know that there is only one result, you don't even need the while loop at all. Just call mssql_fetch_array() once. Link to comment Share on other sites More sharing options...
beefwellington Posted April 29, 2009 Author Share Posted April 29, 2009 mssql_query() returns a unique resource associated with the entire result set.In your broken version, you're sending the query with every iteration, and it's returning a unique result set every time. So every time you call mssql_fetch_array(), it returns the first result. So you're getting the first result, over and over, and it never ends.You only need to send the query one time to get everything you want.As for the working version, if you absolutely know that there is only one result, you don't even need the while loop at all. Just call mssql_fetch_array() once.Thank you for the clarification. Much appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.