/    Sign up×
Community /Pin to ProfileBookmark

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?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@tech7authorSep 13.2010 — 
<?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 />";}


?>

[/QUOTE]


This is where I'm at so far.. Anybody have any suggestions.
Copy linkTweet thisAlerts:
@ChipzzzSep 13.2010 — 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:

<i>
</i>…
$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&gt;'$startdate' AND date&lt;'$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&lt;br /&gt;";
$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']&lt;br /&gt;”;
}


Best of luck ?
Copy linkTweet thisAlerts:
@ChipzzzSep 13.2010 — 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 ?
Copy linkTweet thisAlerts:
@tech7authorSep 13.2010 — check the comment in the middle of my code [/QUOTE]

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..
Copy linkTweet thisAlerts:
@tech7authorSep 13.2010 — 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!!! ?
Copy linkTweet thisAlerts:
@ChipzzzSep 14.2010 — 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,
×

Success!

Help @tech7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...