Jump to content

[SOLVED] MySQL data selection with PHP


FForce2195

Recommended Posts

Greetings.  What a nice place...  I found it yesterday.  I started learning PHP 8 days ago with no programming knowledge.

 

I'm having a bit of problem with MySQL data selection with PHP.  I hope somebody will help me out.

 

I want to extract data where the GMT date is 2 weeks old or newer.  There's a field called comment_date_gmt.  One entry looks like '2007-05-21 00:55:37' without apostrophes.  The table variable name is $table0a.  After accessing a database, I state

 

date_default_timezone_set('UTC');

$current_date = date('Y-m-d H.i:s');

$xdate = date('Y-m-d H.i:s', mktime(0, 0, 0, date("m"), date("d")-14 , date("Y")));

 

If I state the following, I get an error.

 

$sql_date = "select * from $table0a WHERE comment_date_gmt >= $xdate";

 

If I change $xdate to date('Y-m-d', mktime...), the program will run and data will be drawn except that every single row from the table is drawn.  (The data set has a time span of 2008-09-02 to 2008-10-06.)  If I state any date like

 

comment_date_gmt >= 2008-10-01

 

, again, all rows will be drawn.

 

What am I doing wrong?  And how can I correctly enforce data selection where comment_date_gmt >= $xdate (or =< or whichever) ?

 

Thank you for your help.

 

Tom

 

Link to comment
Share on other sites

Great.  Try doing:

 

$sql_date = "SELECT * FROM $tabl0a WHERE comment_date_gmt > DATESUB(NOW(), INTERVAL 14 DAY)";

 

 

Thanks for your help.  You guys are very friendly. 

 

DATESUB(NOW(), INTERVAL 14 DAY) looks like a nice selection method that I could use in a different occasion.  I still get the same error when the program goes through

 

Variable (true or false) = mysql_query(SQL, database connection).

Link to comment
Share on other sites

Yeah, you may get an error with mine, but it should be a more descriptive error, correct?  Could you post it here?  My code would show any errors with the actual query so I could debug it.  Did it show some MySQL error with my code? >_<

 

Ahh...  That's smart.  I'm sorry.  I didn't get it since I'm a novice.  Here it comes.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

 

The MySQL version is 5.0.51a-community in case you needed it.

 

Thanks a lot.

 

By the way, whoever is the administrator, I wish he made an effort in only allowing him/her to see IP addresses.  It's easy to make modifications.  I know how to do it and can help them if needed since I also have an SMF website.

Link to comment
Share on other sites

@FForce: Only mods and admins can see your IP.  You can also see your own IP, but other people can't, don't worry. =P

 

Anyway, the query should be:

$sql_date = "SELECT * FROM $tabl0a WHERE comment_date_gmt > DATE_SUB(NOW(), INTERVAL 14 DAY)";

 

I misspelled DATE_SUB().  I hate it when that happens.

Link to comment
Share on other sites

@FForce: Only mods and admins can see your IP.  You can also see your own IP, but other people can't, don't worry. =P

 

Yes.  I'm sorry.  You are right.

 

$sql_date = "SELECT * FROM $tabl0a WHERE comment_date_gmt > DATE_SUB(NOW(), INTERVAL 14 DAY)";

 

SELECT * FROM table WHERE comment_date_gmt > NOW() - INTERVAL 2 WEEK;

 

Hmm...  I don't know why, but whichever I use, 2 weeks of data will be drawn after I change the interval to 1 week,  4 days or 2 days.  Actually, I have only 2 weeks of day.  So I don't know if it draw the entire data set.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.