Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell driver drivers email error ethernet excel firefox firefox 3 hard drive internet internet explorer itunes laptop linux malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express partition password printer problem router slow software sound spyware trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: PHP & MySQL


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
14-May-2007, 03:03 PM #1
Question Solved: PHP & MySQL
Hey, does anyone know how to adapt this code so that it will pull entries from a specific date range? Preferably, I would like it to pull info for just today. Is there a function for that?
Code:
$sql = "SELECT * FROM wp_posts";
$result = mysql_query($sql,$link) or die("Unable to select: ".mysql_error());
print "<table>\n";
while($row = mysql_fetch_row($result)) {
    print "<tr>\n";
    foreach($row as $field) {
        print "<td>$field</td>\n";
    }
    print "</tr>\n";
}
print "</table>\n";
Here are the fields available in the table:
  • post_author
  • post_date
  • post_date_gmt
  • post_content
  • post_title
  • post_category
  • post_excerpt
  • post_status
  • comment_status
  • ping_status
  • post_password
  • post_name
  • to_ping
  • pinged
  • post_modified
  • post_modified_gmt
  • post_content_filtered
  • post_parent
  • guid
  • menu_order
  • post_type
  • post_mime_type
  • comment_count
Thanks!
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
14-May-2007, 06:10 PM #2
Untested but this should work
PHP Code:
$sql sprintf('SELECT * FROM wp_posts WHERE post_date BETWEEN %s AND %s'strtotime('today'), strtotime('today') + 86400); 
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
15-May-2007, 08:56 AM #3
Thanks Brendan. I gave it a try. I'm not getting any errors, but it does not seem to be pulling any data in the query. Either that, or it's not printing the data. Any other suggestions?
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
15-May-2007, 12:10 PM #4
Using Brendan's suggested modification and
Code:
print ($result);
$result apperantly equals "Resource id #3". Does anyone know what this means?
cpscdave's Avatar
Senior Member with 281 posts.
 
Join Date: Feb 2004
Experience: Intermediate
15-May-2007, 12:29 PM #5
I could very easily be wrong but my experience with SQL is that DATE/DATETIME fields will not work if you pass into them unixtime stamps.
*edit* I tried this out briefly and could not get the query to return correct results using a unix timestamp isntead of actual date *edit*


You actually have to convert them into the correct format.


Code:
$start = strftime("%Y-%M-%D 00:00:00", time()); //will give you todays date at 12:00:00am
$end_timestamp = strtotime("+1 DAY",time()); //will give you time stamp 1 day from now
$end = strftime("%Y-%M-%D 00:00:00", $end_timestamp); //will give you tomoorows date at 12:00:00am

$theQuery = "SELECT * FROM wp_posts WHERE post_date BETWEEN '{$START}' AND '{$END}' ";
__________________
Its not a bug.... Its a feature!

Programming today is a race between software engineers striving to build bigger and better idiot-proof
programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-Rick Cook

Spam a problem for you? http://spamooze.com

Last edited by cpscdave : 15-May-2007 12:34 PM.
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
15-May-2007, 08:42 PM #6
That might be right, I'm not sure if post_date is a DATETIME or a Unix timestamp.
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
16-May-2007, 02:45 PM #7
Thanks for the suggestion, Dave. A thought occured to me. It might be working (no errors), but the query range is off. I'm working from a dummy database right now, so how would I modify the query to pull everything in the last month (or two)? Thanks.
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
16-May-2007, 02:58 PM #8
One more thought:
I checked the settings in the database, and it says that the default format for post_date is "0000-00-00 00:00:00". What is being generated is something like "2007-52-05/17/07 00:00:00". Are these formats the same?
cpscdave's Avatar
Senior Member with 281 posts.
 
Join Date: Feb 2004
Experience: Intermediate
16-May-2007, 03:09 PM #9
Quote:
Originally Posted by pcpro17
One more thought:
I checked the settings in the database, and it says that the default format for post_date is "0000-00-00 00:00:00". What is being generated is something like "2007-52-05/17/07 00:00:00". Are these formats the same?

are you using the same formating string?
to get something like 2007-52-05/25/07 00:00:00 your format string would look like

%Y-%U-%m/%D/%y (I think)

%Y-%m-%d 00:00:00 should give you 2007-05-16 00:00:00 (if today was May 16th 2007)

You can see all the formating elements at:
http://ca3.php.net/strftime

Also if you are using a framework that breaks up the sql statement and recombines it be careful. The one we use had an issue where it would interpret BETWEEN <blah> AND <blah> wrongly and would recombine it wonkly.
__________________
Its not a bug.... Its a feature!

Programming today is a race between software engineers striving to build bigger and better idiot-proof
programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-Rick Cook

Spam a problem for you? http://spamooze.com
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
16-May-2007, 04:12 PM #10
Thanks for the suggestion, Dave! It's now pulling data from the MySQL database! But, this is only half the battle. Next, I need to get it to pull only certain data. Currently, it's pulling everything in the database in the given date rage. Here is what my code looks like now:
Code:
<?php
  include ("__db_open.php");

  $start_timestamp = strtotime ("-30 DAY", time());
  $start           = strftime("%Y-%m-%d %H:%M:%S", $start_timestamp);
  $end_timestamp   = strtotime ("+1 DAY",time());
  $end             = strftime("%Y-%m-%d %H:%M:%S", $end_timestamp);

  $query = "SELECT * FROM wp_posts WHERE post_date BETWEEN '$start' AND '$end'";

  $result = mysql_query($query,$link) or die("Unable to select: ".mysql_error());

  print "<table>\n";
  while($row = mysql_fetch_row($result)) {
    print "<tr>\n";
    foreach($row as $field) {
        print "<td>$field<br>$row</td>\n";
      }
    print "</tr>\n";
    }
  print "</table>\n";

  mysql_close($link);
?>
If you'd like to see what it's doing, go here:
http://www.correctnesscommentary.com/__test.php

Any suggestions on how to get it to pull data for only post_date, post_content, and post_title?

Thanks again!

Last edited by pcpro17 : 17-May-2007 02:12 PM.
cpscdave's Avatar
Senior Member with 281 posts.
 
Join Date: Feb 2004
Experience: Intermediate
16-May-2007, 05:50 PM #11
Quote:
Originally Posted by pcpro17
Any suggestions on how to get it to pull data for only post_date, post_content, and post_title?

Thanks again!

What version of MySQL are you using?? I dont belive BETWEEN worked correctly with dates till 4.3.x
Also print out $query just before you send it to mysql it should look something like
SELECT * FROM wp_posts WHERE post_date BETWEEN '2007-04-16 15:41:37' AND '2007-05-17 15:41:37'

What format is post_date stored in the database? are they of type datetime? if they are a string or some other format this will not work (and you should porbally convert the field to a datetime)

as far as only having the data base return specific fields change your query from:

SELECT * FROM table WHERE blah;

TO

SELECT field1, field2, field3 FROM table WHERE BLAH
__________________
Its not a bug.... Its a feature!

Programming today is a race between software engineers striving to build bigger and better idiot-proof
programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-Rick Cook

Spam a problem for you? http://spamooze.com
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
17-May-2007, 02:09 PM #12
Hello Dave. The MySQL client version is 5.0.16. I've attached a screen shot of the wp_posts table. I think it has the formatting info you requested.
Attached Thumbnails
solved-php-mysql-database.jpg  
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
17-May-2007, 02:12 PM #13
Oh, if I'd built this database myself, I'd have no issue with changing the formats of stuff, but this database was generated by the WordPress setup, and because I'm planning on using the default WordPress interface to make updates, I don't want to do anything that might break it.
pcpro17's Avatar
Senior Member with 245 posts.
 
Join Date: Oct 2006
Location: Wisconsin, USA
Experience: Advanced
17-May-2007, 02:14 PM #14
Oops, I also noticed a typo on my previous post. The code posted does pull data without any errors, but it's pulling everything within the date range. I just want it to pull only post_date, post_content, and post_title for now.
cpscdave's Avatar
Senior Member with 281 posts.
 
Join Date: Feb 2004
Experience: Intermediate
17-May-2007, 02:28 PM #15
Oh that makes life a lot easier

Change
Code:
 $query = "SELECT * FROM wp_posts WHERE post_date BETWEEN '$start' AND '$end'";
TO

Code:
 $query = "SELECT post_date, post_content, post_title  FROM wp_posts WHERE post_date BETWEEN '$start' AND '$end'";
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:33 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.