Jump to content

Question About Mssql_fetch_array


beefwellington
 Share

Recommended Posts

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

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.

Edited by Deirdre's Dad
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...