khizer Posted July 21, 2012 Share Posted July 21, 2012 Hello every one, I am a php beginner and have this quick question: The following code works fine: $res = mysql_query('SELECT value from stock_config where name="sale_type"'); while( $res_st = mysql_fetch_array($res) ){ $sale_types[] = $res_st['value']; } print_r($sale_types); But the following code causes white error page: while( $res_st = mysql_fetch_array(mysql_query('SELECT value from stock_config where name="sale_type"')) ){ $sale_types[] = $res_st['value']; } print_r($sale_types); I cannot figure out why. Why can't this: $res = mysql_query('SELECT value from stock_config where name="sale_type"'); while( $res_st = mysql_fetch_array($res) ) be replace with: while( $res_st = mysql_fetch_array(mysql_query('SELECT value from stock_config where name="sale_type"')) ){ Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/266030-question-about-mysql_query/ Share on other sites More sharing options...
Barand Posted July 21, 2012 Share Posted July 21, 2012 Your first (correct) version gets a result set then loops through the rows. When there are no more rows the fetch returns false and the loop ends. In your second version you are continually getting and refreshing the result set and reading the first row - so it never ends. Quote Link to comment https://forums.phpfreaks.com/topic/266030-question-about-mysql_query/#findComment-1363223 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.