Jump to content

What have I done wrong?


TomFromKWD

Recommended Posts

I am trying to get the SELECT query to take a date from a field in the database called displaydate and ensure it only shows entries that have a displaydate of the date today or earlier

<?PHP
mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y-%m-%d' as thedate FROM `2011-09` WHERE DATE_SUB(CURDATE(),INTERVAL 31 DAY)  ORDER by day_id DESC");
?>

 

the above code is just kicking out an error so I assume I've got lost somewhere but not sure where

 

can someone please help me correct what I have wrong and explain WHY it is wrong

Link to comment
Share on other sites

the error is as follows

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in "path"/2011-09.php on line 49

 

 

and unfortunately the issue persists after the above supplied code

 

could it be to do with how I am storing the date in question in the database?

 

 

and if it helps the full bit is now as follows:

 

<?php

                           include 'connection.php';

                           $getimages=mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y %m %d') as thedate FROM `2011-09` WHERE thedate <= CURDATE() ORDER by day_id DESC");

                           while ($images=mysql_fetch_array($getimages)) {
                           echo " " .$getimages['codeopen']. "" .$getimages['code']. "" .$getimages['codeclose']. ""; }
                         ?> 

Link to comment
Share on other sites

it is there though, and without the date alterations im trying to make and just using the following alone works, but obviously is showing EVERYTHING in the table, including that set with a date for the future

 

$getimages=mysql_query ("SELECT * FROM `2011-09` ORDER by day_id DESC");

Link to comment
Share on other sites

my mistake.. that error simply means that you r query is failing.. add a die() after the query to receive a specific error..

 

mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y-%m-%d') as thedate FROM `2011-09` WHERE thedate <= CURDATE() ORDER by day_id DESC") or die(mysql_error());

Link to comment
Share on other sites

im not an expert at mysql, don't try to be.. however something is going wrong with your alias of the displaydate field.. so try removing the alias..

 

mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y-%m-%d') FROM `2011-09` WHERE displaydate <= CURDATE() ORDER by day_id DESC") or die(mysql_error());

Link to comment
Share on other sites

the where clause is parsed and evaluate prior to the select clause. Because of this the aliasing of thedate to where clause not yet occurred

$getimages=mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y %m %d') as thedate FROM `2011-09` WHERE DATE_FORMAT(displaydate,'%Y %m %d')<= CURDATE() ORDER by day_id DESC");

 

Link to comment
Share on other sites

$getimages=mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y %m %d') as thedate FROM `2011-09` WHERE DATE_FORMAT(displaydate,'%Y %m %d')<= CURDATE() ORDER by day_id DESC");

 

Running the date_format twice is extra overhead.  Just access the column.

 

$getimages=mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y %m %d') as thedate FROM `2011-09` WHERE displaydate <= CURDATE() ORDER by day_id DESC") or trigger_error(mysql_error());

Link to comment
Share on other sites

mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y-%m-%d') FROM `2011-09` WHERE displaydate <= CURDATE() ORDER by day_id DESC") or die(mysql_error());

 

 

the above worked i just forgot to put my &getimages= on the front like a fool

 

Thanks for all the support guys

 

Youve been immense as always

Link to comment
Share on other sites

mysql_query ("SELECT *,DATE_FORMAT(displaydate,'%Y-%m-%d') FROM `2011-09` WHERE displaydate <= CURDATE() ORDER by day_id DESC") or die(mysql_error());

 

 

the above worked i just forgot to put my &getimages= on the front like a fool

 

Thanks for all the support guys

 

Youve been immense as always

I was surprised when you said the code i provided didn't work at first.. simple error glad it worked for you

Link to comment
Share on other sites

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.