Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Issue with MYSQL Select and CURDATE()

    EDIT: I'm new here and for some reason, it looks like I created two threads. How do I delete one?

    Hi, I have a mysql select from database when the date is today that I then put into a javascript text rotator. Unfortunately, it isn't displaying anything. Why would that be?
    PHP Code:
    include("config.php");
    $result = mysql_query("SELECT * FROM events WHERE 'when' = CURDATE()");
    if($result === FALSE) {
        die(mysql_error()); // TODO: better error handling
    }

    if ($result) {
      // array to hold all the output
      $events = array();
      while ($row = mysql_fetch_assoc($result)) {
        // Add the event to your array
        $events[] = $row['tag'];
      }
      // After building the array, encode it as JSON
      // Later you'll echo this into your JavaScript in place of the array...
      $events = json_encode($events);
    }
    echo $events;
    ?>
    <script type='text/javascript'>
    function rotateEvery(sec)
    {
      // The JSON from PHP output here
      // Would look something like
      // ["Event 1","Event 2","Event 3"]
      var Quotations = <?php echo $events?>;

      var which = Math.round(Math.random()*(Quotation.length - 1));
      document.getElementById('textrotator').innerHTML = Quotation[which];

      setTimeout('rotateEvery('+sec+')', sec*1000);
    }
    </script>
    Last edited by infinity0; 07-13-2012 at 04:31 PM. Reason: made two threads

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    I deleted the other thread.
    Welcome to CodingForums.

    I can tell you that if your query is actually like this:
    "SELECT * FROM events WHERE 'when' = CURDATE()

    you have two problems:
    1) WHEN is a reserved word so really you should have a different column name than that
    2) by putting single quotes around your column name you have created a string, the word "when", the word "when" will never match a date so you will never get results.

    You need backticks ` which is under the ~ key on the top left of your keyboard, and not single quotes as you have them. Better yet, give the column a new name and you won't need them at all.

  • Users who have thanked guelphdad for this post:

    infinity0 (07-13-2012)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This condition is invalid: 'when' = CURDATE(). 'when' is literally the string 'when'. Since when is a reserved word for case handling, you need to change the condition to `when` = CURDATE(), assuming its a datetime type of course.

    Edit:
    Nuts, beaten to the punch :P

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Quote Originally Posted by Fou-Lu View Post
    Nuts, beaten to the punch :P
    I am in Ontario after all.

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thanks!

    Worked Great, thanks so much.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •