So I have two tables - Horses: List horses information Results: Lists results of shows with horses in them
I have a view: v_testhorses: has information from Horses listed, then calculates amount of 1st (wins), 2nds (show), 3rds (places) and earnings of a horse per YEAR. For each YEAR there's a different line in the table, so that the earnings, places, etc. can be displayed by year
I'm trying to query this on a search result page and I'm having trouble figuring out HOW to query this information. Ideally it'd be into an HTML table like so- Year - wins - place - shows - LTE
However, not EVERY horse will have shown in 2017, 2016, etc. so I would like for those years to be blank
Current code:
$data=mysql_query("SELECT * FROM `v_testhorses` WHERE `year`=2017"); $row=mysql_fetch_object($data); echo mysql_error(); echo"<tr><td>$row->year</td>"; echo"<td>$row->wins</td>"; echo"<td>$row->place</td>"; echo"<td>$row->show</td>"; echo"<td>$" . number_format($row->LTE) . "</td></tr>"; -I've tried to have the "echo" with isset, empty, etc and no matter which of the million ways I try it, I get some sort of error. Either it crashes my entire page or it does nothing -I've tried switching my query to an IF statement (IF year = 2015, for example) to no avail
QUESTION: 1. How should I query this where if there's no entry for the horse when year = 2015 it just doesn't pull anything up? 2. Is there an easier way to query this than copy and pasting the above code (or the fixed version of it!) over and over for every year 2007-2017? And so when 2018 rolls around I don't have to update the code. (many thanks in advance!)