Jump to content

question about mysql_query


khizer

Recommended Posts

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,

Link to comment
Share on other sites

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.

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.