PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm using PHP & MySQL to select a blog entry based on it's publish date. I've omitted the unneeded code. Will the part where it says "WHERE date = $today" work or do I need to wrap $today with single or double quotes?
$today = date("Y-m-d h\:i\:s");
$result = mysql_query("SELECT * FROM blogEntries WHERE date = $today");
if (!$result) {
die('There was an error with MySQL: ' . mysql_error());
}
$row = mysql_fetch_row($result, MYSQL_ASSOC);
Hi,
you do not need to escape the : in your date command:
$today = date("Y-m-d h:i:s");
But yes, you have to use simple quotes in your sql statement, otherwise the SQL statement looks like:
SELECT * FROM blogEntries WHERE date = 2008 11 1 17:45:30
that won't work.
SELECT * FROM blogEntries WHERE date = '$today'
And you do realize that it will only retrieve a result if the result exists at the same exist second that the current page is being loaded. If you are indeed trying to do that because of a row you just inserted you will run into errors down the road.