Page 1 of 1

Limit Function Question

Posted: Sat Nov 22, 2008 8:42 pm
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.

Re: Limit Function Question

Posted: Sat Nov 22, 2008 11:08 pm
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!

Re: Limit Function Question

Posted: Sun Nov 23, 2008 8:42 pm
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())