Is my syntax correct?

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!

Moderator: General Moderators

Post Reply
User avatar
SilvaMo0n
Forum Newbie
Posts: 2
Joined: Sun Nov 02, 2008 10:12 am
Location: United States

Is my syntax correct?

Post 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);
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: Is my syntax correct?

Post 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'
bmoyles0117
Forum Newbie
Posts: 22
Joined: Sun Nov 02, 2008 1:46 am

Re: Is my syntax correct?

Post 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.
User avatar
SilvaMo0n
Forum Newbie
Posts: 2
Joined: Sun Nov 02, 2008 10:12 am
Location: United States

Re: Is my syntax correct?

Post 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. :)
Post Reply