0

I'm trying to select data from a specific table in my database, but I want to be able to only view the last 3 days worth of data, I have the following code but for some reason I can't get it to work :(

$result = mysqli_query($con,"SELECT * FROM demands WHERE t.date >= DATE_ADD(CURDATE(), INTERVAL -3 DAY)");

I am getting the following error; Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in

Any help is appreciated!

1 Answers1

3

You may avoid usage of DATE_ADD() at all:

SELECT * FROM demands as t WHERE t.date >= (CURDATE() - INTERVAL 3 DAY)

As @OGHaza mentioned, you specified column with alias to nowhere: t.date should be just date (note that it is a reserved word, so you should use backticks around it in this case) or demands should be specified with an alias like demands as t.

BlitZ
  • 11,836
  • 3
  • 49
  • 64
  • I'm still getting the same error unfortunately :( Could it possibly be row? `while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['submitted'] . ""; echo "" . $row['optone'] . ""; echo "" . $row['opttwo'] . ""; echo "" . $row['optthree'] . ""; echo "" . $row['product'] . ""; echo "" . $row['dept'] . ""; echo ""; } echo "";` –  Oct 23 '13 at 16:05
  • @AspiringProgrammer try to debug your query: `mysqli_query($con, 'query') or die(mysqli_error($con));`. – BlitZ Oct 23 '13 at 16:07
  • I'm not sure if it can be done differently, but what I'm trying to achieve is to be able to view all data from a specific table, but give the user the ability to filter the information by date (dropdown, link, search box etc) –  Oct 23 '13 at 16:08
  • Old question now (came up in review) but is the problem not just that he was referencing t.date with nothing aliased as t :S – OGHaza Jan 10 '14 at 13:55
  • @OGHaza Updated, Thanks. – BlitZ Jan 11 '14 at 09:42