0

I have a database with a calendar table (each row represents one day) with 4 years of rows (2012, 2013, 2014, 2015). I use the column name calyear for the year.

I use the following code to find values for distinct years then display it:

$year = mysql_query("SELECT DISTINCT calyear FROM calendar");

while($yeararray = mysql_fetch_array($year))
{
 echo($yeararray['calyear']."<br />");
}

The problem is it only displays the years 2013, 2014, 2015 even though when I use

echo(mysql_num_rows($year);

it displays the value 4 which I take to mean all 4 years are there. I'm not quite sure where I'm going wrong with this.

*EDIT*

Thanks for your suggestions but I found the problem and it was my own stupidity.

I had the $year query at the top of the page and the While loop later on (where I needed it). I'd earlier put $yeararray = mysql_fetch_array($year) directly underneath the $year so I had it twice on the page, and when I removed the redundant one at the top the code worked fine.

Thanks and sorry!

2
  • 1
    maybe the database is returning those three years plus null in the fourth row? it's the only thing I can think of given your description. Commented Sep 12, 2012 at 21:37
  • use var_dump to check whats in variables ... Commented Sep 12, 2012 at 21:43

1 Answer 1

1

Maybe the 4th value is a null or blank, and when you echo it, you simply don't see it.

Do a desc calendar and find out what default value is set for column calyear.

Then do a select count(*) from calendar where calyear is null or a select count(*) from calendar where calyear = <whatever the default you've set> to see if it's a blank or null value that's being picked up.

Sign up to request clarification or add additional context in comments.

1 Comment

But if I take away the While loop and just have echo($yeararray['calyear']) it displays only 2012. 2012 is being lost somewhere when I put it in the While loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.