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
https://forums.phpfreaks.com/topic/127296-solved-mysql-data-selection-with-php/
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).

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? >_<

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.

@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.

@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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.