Page 1 of 1

Is my syntax correct?

Posted: Sun Nov 02, 2008 10:15 am
by SilvaMo0n
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?

Code: Select all

$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);

Re: Is my syntax correct?

Posted: Sun Nov 02, 2008 10:42 am
by Hannes2k
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'

Re: Is my syntax correct?

Posted: Sun Nov 02, 2008 10:57 am
by bmoyles0117
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.

time() will put out the same exact timestamp.

Re: Is my syntax correct?

Posted: Sun Nov 02, 2008 11:29 am
by SilvaMo0n
Thank you bmolyes0117. And thank you Hannes2k, that would have caused me a lot of problems. I'll change what I need to change, again thank you. :)