Limit Function Question

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
SChaput
Forum Newbie
Posts: 1
Joined: Sat Nov 22, 2008 8:38 pm

Limit Function Question

Post by SChaput »

I am making a script in which a person is able to post things to an online weblog. The user submits a form and its added to the database. I am using this code to retrieve data from my database, and put it on my main page.

Code: Select all

 
 mysql_connect("server","","");
    mysql_select_db("");
   $query ="SELECT type, poster, during, time";
   $query.=" FROM ws ORDER BY time DESC LIMIT 20";
   $result=mysql_query($query);
   while (list($type,$poster,$during,$time) = 
    mysql_fetch_row($result)) {
 
However, instead of 'LIMIT 20' i would like the limit to be only posts that were made on the current day. How do i accomplish this?
If i were to add another field to my database like the current date, November 22, 2008, that would then be attributed to each row that was created on that day. Would i be able to set my LIMIT function to something like this?

Code: Select all

 
<?php
$today = date("l, F j, Y");
   $query.=" FROM ws ORDER BY time DESC LIMIT $today";
?>
 
Thus, allowing me to call only the rows that were posted on November 22, 2008?
Thank you.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Limit Function Question

Post by novice4eva »

You shouldn't be looking at limit at all then. Just add a condition after WHERE "date=curdate()" and remove limit part. Limit is for limiting number of rows returned, that's it!
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Limit Function Question

Post by sparrrow »

I agree you should have a CREATED column in your table with a datetime type, and when posts are created you should use the mysql function now() as the value. Then you can have the power to filter by any date or time range you like.

Code: Select all

INSERT INTO table (id, poster, text, created) values (0, 'joe', 'hello world', NOW())
Post Reply