By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

Error with while Loop... very confusing!

P: 35
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies.

I am using PHP5 with mysql. Same problem in Fx 2.0 and IE 7.
I have the query sort of spread out across three pages to make it easier for me to generate it; I have a pagination script which checks what is being accessed from the query string (is it the whole blog or an individual entry?). This was all working great until last night. My data is there, and other queries to the db are working FINE; my tag cloud and 'newest articles' header are both functioning.
But my main page is simply dying.

Here is the code used, from the different pages, all at once. I have echoed everything out and I can find no errors.

[php]//from nav.setup.php
$page = 1;
$max_results = 10;
$from = (($page * $max_results) - $max_results);
$nav_sql = "SELECT * FROM `blog` ORDER BY `blogId` DESC LIMIT $from, $max_results";
$nav_sqlcount = "SELECT count(*) AS num FROM blog";
//die('Died at default: '.$sql.'<br />'.$sqlcount);

//from pagination.php
$getcount = mysql_query($nav_sql) or die('problem getting count: '.mysql_error());;
$total_results = mysql_result($getcount, 0) or die('problem getting total results: '.mysql_error());;
$total_pages = ceil($total_results / $max_results);
$nav_result = mysql_query($nav_sql) or die('problem getting result: '.mysql_error());
$fr = $from + 1;
$to = $from + mysql_num_rows($nav_result);

//from main page
echo "<div id=\"blogDiv\">

<div>Newest Articles</div>
<div class=\"spacer\">&nbsp;</div>";
$new_article_query = mysql_query("SELECT * FROM blog ORDER BY blogId DESC LIMIT 5");
while($new_article_row = mysql_fetch_row($new_article_query)) {
$titleLink2 = preg_replace('/ {1,}/', '_', trim($new_article_row[4]));
echo "<div><a href=\"{$new_article_row[0]}/$titleLink2\">{$new_article_row[4]}</a></div>";
echo "<div class=\"spacer\">&nbsp;</div>";

echo $nav_sql;
$blog_result = mysql_query($nav_sql) or die('problem getting result for blog: '.mysql_error());

while($blogArray = mysql_fetch_array($blog_result) or die("Problem fetching array: ".mysql_error())) {
anything here always dies;
echo "</div>
<div class=\"spacer\">&nbsp;</div>";

The echoed query looks fine.

I removed all the info between the while loop because it doesn't matter what I put there, it simply dies. If I take the while loop out, it is blank (of course), but at least doesn't die and gets to the rest of the code (footer etc). This is what has led me to believe that it is a problem with the while loop.

ANY ideas greatly appreciated!

To see the error in action:
You'll also see there that EVERYTHING else works fine. Anything, please :D

Feb 15 '07 #1
Share this Question
Share on Google+
2 Replies

P: 35
Well, it seems I can't delete this thread, so I guess I'll provide some closure. I solved the problem. I stupidly put "or die(mysql_error())" at the end of a couple loops which would return 1 or less results and so the loop wouldn't really need to loop (mysql_num_row which had 0 to loop through and so it would die). this seemed to be the problem, but it was VERY confusing. Thanks to anyone that looked the thread. Hopefully I don't make such a dumb mistake again :P
Feb 16 '07 #2

Expert 2.5K+
P: 4,258
There are no dumb mistakes! Everyone makes a mistake some time, so don't worry. Part of life. Good luck!

ronald :cool:
Feb 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.