Jump to content

Recommended Posts

Ok guys this one is a strange one. I know that this is not a mysql issue because mysql is not outputting any errors. I believe it is in the while loop, however I have never seen a while loop act like this.

 

here is a link to the page http://www.jamsmagazine.com/index2.php?section=news

 

and here is my code

        <p><img src="images/jams-magazine-news.gif" width="150" height="20"></p>
<?php
//Get database connectiong config
include('config.php');

$query = "SELECT * FROM jamsmag_news ORDER BY id ASC LIMIT 4";
$result = mysql_query($query) or die(mysql_error());
//Get article from db
$count = "0";
while ($row = mysql_fetch_assoc($result) or die(mysql_error())) {
	$title = $row['title'];
	$article = $row['article'];
	echo $title;
	echo $article;
	echo $count;
	$count = $count + 1;

}
?>

 

Now it is repeating the second news article "No more Gorillaz studio albums" is being repeated. whats more the first article is not displaying the counter echoed. What is causing this odd behavior any ideas, there are only 4 records in the news table so this makes no sense.

 

Thanks :facewall: :facewall: :facewall: :facewall: :hammer_time::psychic::suicide:

Link to comment
https://forums.phpfreaks.com/topic/172006-extra-itteration-through-loop/
Share on other sites

Remove the or die(...) from the end of the mysql_fetch_assoc(). That makes no sense for a couple of reasons. msyql_fetch_assoc() does not produce a mysql error, it fetches rows from the result set and when there are no more rows in the result set a FALSE is returned so your code will die() at that point with nothing being output following your loop.

 

For any other problems, you should directly examine the data in your table because it is probably not what you think it is.

well if you are going to preform math on count, you want to it be an integer, not a string (which it currently is) so change

$count = "0";

to

$count = 0;

 

you can also change

$count = $count + 1;

to

$count++;

its a little easier

 

 

but i doubt that has anything to do with your error

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.