Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-13-2012, 04:27 PM   PM User | #1
infinity0
New to the CF scene

 
Join Date: Jul 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
infinity0 is an unknown quantity at this point
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
infinity0 is offline   Reply With Quote
Old 07-13-2012, 04:46 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
infinity0 (07-13-2012)
Old 07-13-2012, 04:47 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 07-13-2012, 04:54 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Quote:
Originally Posted by Fou-Lu View Post
Nuts, beaten to the punch :P
I am in Ontario after all.
guelphdad is offline   Reply With Quote
Old 07-13-2012, 05:10 PM   PM User | #5
infinity0
New to the CF scene

 
Join Date: Jul 2012
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
infinity0 is an unknown quantity at this point
Thanks!

Worked Great, thanks so much.
infinity0 is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:59 PM.


Advertisement
Log in to turn off these ads.