Jump to content

[SOLVED] php question regarding while statement


SchweppesAle

Recommended Posts

hi, this is probably a really simple question.  Up until now I've just been using the following method in order to loop through a mysql query.

 

mysql_select_db("account6_jo152");
$result = mysql_query("SELECT * FROM blahblah WHERE yadayada = '0'");

while($row = mysql_fetch_array($result))
{
/*manipulate some column like so for each entry*/
$row['somecolumn'];
}

 

I understand that the mysql_fetch_array() method is storing the query within an array named $row; what confuses me is how this statement is incrementing after each iteration. 

 

Shouldn't while statements simply continue to loop the same thing over and over again until the condition no longer holds true?  How is it cycling through each element in the array?

that is just how mysql_fetch_array() works. it increments on each call. so doing this:

$row = mysql_fetch_array($result);
$row = mysql_fetch_array($result);
$row = mysql_fetch_array($result);

will also increment each time

 

http://us3.php.net/mysql_fetch_array

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

it will ALWAYS return a value so the while loop takes that as TRUE, the when there are no more results mysql_fetch_array returns FALSE, and your loop ends.

 

Return Values

 

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

that is just how mysql_fetch_array() works. it increments on each call. so doing this:

$row = mysql_fetch_array($result);
$row = mysql_fetch_array($result);
$row = mysql_fetch_array($result);

will also increment each time

 

http://us3.php.net/mysql_fetch_array

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

 

that was actually really helpful.  I think I get it now, thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.