www.webdeveloper.com
Results 1 to 7 of 7

Thread: Date functions in a mysql_fetch_array

  1. #1
    Join Date
    Sep 2010
    Posts
    4

    Date functions in a mysql_fetch_array

    Hi, Sorry if this is infantile, I'm a noob..
    I'm trying to write a sort of "event list" for my dad's website and I can't figure out what kind of function I need to include in the code that would select or display only table rows that occur after today's date.
    Does anybody have or know where I can find insight into this that would help me complete this?

  2. #2
    Join Date
    Sep 2010
    Posts
    4
    <?php

    $username="root";
    $password="password";
    $database="test";
    $today = date("Y-m-d");

    //connection
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    //query
    $query=mysql_query("SELECT * FROM testcal WHERE $date > $today") or die('Query failed: ' . mysql_error());


    //results
    while ($row = mysql_fetch_array($query)) {
    echo
    "<br /> Date:"
    .$row['date'].
    "<br /> Location:"
    .$row['location'].
    "<br /> City/State:"
    .$row['citystate'].
    "<br />";}


    ?>
    This is where I'm at so far.. Anybody have any suggestions.

  3. #3
    Join Date
    Apr 2010
    Posts
    213
    The question is not infantile at all and the answer has some subtleties that can lead to interesting insights. The difference between a date and datestamp, for example... but you'll discover that as you go along. To get you started, though, you do the hard part in the selection statement like this:

    Code:
    …
    $link = mysql_connect($host, $user, $password) or die(“Could not connect to database: ” . mysql_error());
    mysql_select_db($dbtable, $link) or die('Uh oh...' . mysql_error());
    $query = “SELECT * FROM table WHERE date>'$startdate' AND date<'$enddate'”;
    // Uncomment the next line if you run into problems that don't generate an error - your query may not look like you think it does
    // echo "$query<br />";
    $result = mysql_query($query);
    if ($result === FALSE) die('Whoopsie: ' . mysql_error());
    while (($thegoodstuff = mysql_fetch_assoc($result)) !== FALSE) {
      echo “$thegoodstuff['date'], $thegoodstuff['anotherfield'], $thegoodstuff['etc']<br />”;
    }
    Best of luck

  4. #4
    Join Date
    Apr 2010
    Posts
    213
    lol... you sent the second message while I was still composing mine... anyway, check the comment in the middle of my code (seeing your query often resolves the problem)

    Good luck

  5. #5
    Join Date
    Sep 2010
    Posts
    4
    Quote Originally Posted by Chipzzz View Post
    check the comment in the middle of my code
    Wow, I've never thought of that.. that's awesome.. I'm definetly going to use that a lot from now on.. I'm still working through this code, I'll report back here after a bit.. After looking at your code, I think I see where I'm going wrong..

    thanks a mil!! I'll brb..

  6. #6
    Join Date
    Sep 2010
    Posts
    4
    hey thanks chipzzz, I got it figured out now.. I've worked on this stupid thing for two days.. I'm real glad you replied to me!!! thanks so much!!!

  7. #7
    Join Date
    Apr 2010
    Posts
    213
    Glad to hear you worked it out & glad I could help. Printing out intermediate results is a real time saver in debugging. The more you do it, the more you will recognize its value.

    Have a nice day,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles