Comparing year not working

I have inherited a site and am trying to go from knowing zero about PHP as quickly as possible and hope you can help. We had an upgrade to our SQL database recently which changed the way SQL allows you to create columns as it moved to explicit mode. So we had to change a column that is storing the year.

  • tourn_year - type year (4), Null No, Default None

I have managed to fix most of the php code we have managed to fix except this one…

The code is trying to calculate is whether the record we are reading in from our SQL DB which contains a 4 digit year <> to the current year (first paragraph below).

It is then meant to display the records it finds which have a year <> to this year into a page of that year (“previous years”) (second paragraph below). But all we get is all the records in a page dated year 0 ??

I was hoping to use the echo statement to see what year was being picked up from the query below in the first paragraph but when I add "echo $tyear; I get the “Resource id 5” ???

Any help troubleshooting this would be appreciated

 mysql_select_db($database_connvbsa, $connvbsa); 
 $query_tyear =   "SELECT YEAR (tourn_year) AS Tyear FROM tournaments WHERE YEAR(tourn_year) <> YEAR( CURDATE( ) )   GROUP BY Tyear ORDER BY Tyear DESC"; 
 $tyear = mysql_query($query_tyear, $connvbsa) or die(mysql_error()); 
 $row_tyear = mysql_fetch_assoc($tyear); 
 $totalRows_tyear = mysql_num_rows($tyear); 
     <table align="center" cellpadding="5" cellspacing="5">  
       <tr> 
         <td>View tournaments from previous years</td> 
       </tr> 
       <?php do { ?> 
         <tr> 
           <td align="center" class="greenbg" >  <a href="aa_tourn_index_history.php?tourn_year=<?php echo $row_tyear['Tyear']; ?>"><?php echo $row_tyear['Tyear']; ?  ></a></td> 
         </tr> 
         <?php } while ($row_tyear = mysql_fetch_assoc($tyear)); ?> 
     </table> 
     <p>&nbsp;</p> 
     </body> 
     </html> 
     <?php 
     mysql_free_result($tourn1); 

     mysql_free_result($tourn2); 

     mysql_free_result($tyear); 

     mysql_free_result($tourn_closed); 
     ?> 

Can you include your table structures, and some sample data from your tables?

Please see here. As you can see some of the events are listed 2020 (i.e. <> Current year

Your tourn_year column is already in the format you need, you don’t need to apply the YEAR() function to it. Applying the YEAR() function to a four digit year (such as 2020) will give you back null; null <> "2020" evaluates to false in MySQL.

Change your query like so:

SELECT tourn_year FROM tournaments 
WHERE tourn_year <> YEAR(CURDATE( ))
GROUP BY tourn_year
ORDER BY tourn_year DESC

Thanks this has worked (to a degree, see below). But first (and please forgive my lack of knowledge) I noticed our code has an “AS Tyear” after the SELECT command and notice you left this out. Is that an oversight or is it no longer required?

Your change produced two green buttons one for 2020 and one for 0000 as there are a few records in the DB table for 2020 and the rest have no date. But they had no value in them so you could only see a small green blob but and they had the same list of events.

I then modified the section of code (second paragraph I sent you in the original posting) where it stated Tyear twice to state tourn_year for the a href=“aa_tourn_index_history.php?tourn_year=<?php echo $row_tyear['Tyear']; ?>”><?php echo $row_tyear[‘Tyear’]; ?

This put 0000 in one of the green buttons and 2020 in the other green button :grinning:
The values being passed in the url are correct 0000 and 2020

I then modified the aa_tourn_index_history.php which has the year parameter passed to it by the code I modified above by doing what you suggested which is to not use
$row_tyear [Tyear] but use $row_tyear [tourn_year]

We are very nearly there but not quite. The small error exists aa_tourn_index_history.php
As I can’t post more code in one post I’ll make a new post next called “YEAR problem nearly there”.

Thanks very much for your great help.

Sponsor our Newsletter | Privacy Policy | Terms of Service