Jump to content

Recommended Posts

Can someone explain while loops in numpty language to me?

 

I'm trying to cycle through news items fetched from a table in the code below, but I don't really understand what the while part does:

 

while ($getNews = mysql_fetch_array($result))

 

I always see mysql_fetch_array in while loops - but what does the loop actually do?  Does it automatically increment or something?  If so, how does it know what to increment (I'm trying to get it to loop down the rows, but why not the columns or every piece of data?)

 

 

$query = 	("SELECT news_id, 
						category, 
						title, 
						text, 
						img_url, 
						linkto, 
						ext_url, 
						DATE_FORMAT(date,'%d %M, %Y') as sd 
			FROM 		news_posts
			ORDER BY 	date 
			DESC LIMIT 	0, 3") 

			or DIE ("Can't retrieve");

$result=@mysql_query($query) or die("Error in News");


<?php

while ($getNews = mysql_fetch_array($result))
{					
echo "<div id='hiddenNews1' style='display:none;'>
<a href='#'><img src='".$site.$getNews['img_url']."' width=292 height=176 /></a>
</div>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/84763-dont-understand-while-loops/
Share on other sites

The while loop runs as long as the object in its parentheses is not false or 0. mysql_fetch_array returns an array if there are results or false if there are none. Once while($getNews = false) the while loop stops because it hits false or 0.

 

Not quite the right explanation. Look at mysql_fetch_array($result). This is an array. So we have an array of 'n' items, and we will go through each index in the array, until we can't go to the next one (n+1 item). At this point, it returns a false error as that array value doesn't exist anymore.

Try changing:

$query = 	("SELECT news_id, 
						category, 
						title, 
						text, 
						img_url, 
						linkto, 
						ext_url, 
						DATE_FORMAT(date,'%d %M, %Y') as sd 
			FROM 		news_posts
			ORDER BY 	date 
			DESC LIMIT 	0, 3") 

			or DIE ("Can't retrieve");

 

To:

$query = 	"SELECT news_id, 
						category, 
						title, 
						text, 
						img_url, 
						linkto, 
						ext_url, 
						DATE_FORMAT(date,'%d %M, %Y') as sd 
			FROM 		news_posts
			ORDER BY 	date 
			DESC LIMIT 	0, 3";

 

And if you're only using the img_url column, you don't have to select all that.

 

$query = 	"SELECT img_url
			FROM 		news_posts
			ORDER BY 	date 
			DESC LIMIT 	0, 3";

The while loop runs as long as the object in its parentheses is not false or 0. mysql_fetch_array returns an array if there are results or false if there are none. Once while($getNews = false) the while loop stops because it hits false or 0.

 

Not quite the right explanation. Look at mysql_fetch_array($result). This is an array. So we have an array of 'n' items, and we will go through each index in the array, until we can't go to the next one (n+1 item). At this point, it returns a false error as that array value doesn't exist anymore.

 

And neither is that. Each time mysql_fetch_array() is called with a result resource as its argument it returns an array representing a database row, it then moves the internal pointer forward one position. It will keep doing this until there are no more rows within the result set, where it will then return false.

 

Placing a call to mysql_fetch_array within a while loop means it will fetch each row of the given result resource and then exit the loop once the rows are exhausted.

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.