Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2012, 02:01 PM   PM User | #1
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
mysql Select timestamp

Hello
I am trying to select must commented thread of the week, and I have tried a few ways, but cant seem to get it working.

Hope you can help...



PHP Code:
$sql "SELECT thread_id,thread_name,comment_timestamp, COUNT(thread_name) AS p_count FROM comment WHERE comment_timestamp >= CURRENT_timestamp - INTERVAL 7 DAYS GROUP BY thread_name ORDER BY p_count DESC LIMIT 0,1";
$result mysql_query($sql$conn) or die(mysql_error());
while (
$newArray mysql_fetch_array($result))
 {
$thread_name $newArray[thread_name'];
$thread_id = $newArray[thread_id'
];
$p_count $newArray['p_count'];
echo 
"<center><a href=thread_view.php?thread_id=$thread_id'>$thread_name</a><br>Comments ($p_count)<br><br></center>";

soupy1985 is offline   Reply With Quote
Old 01-12-2012, 09:41 PM   PM User | #2
XmisterIS
Regular Coder

 
Join Date: Jun 2010
Posts: 239
Thanks: 50
Thanked 6 Times in 6 Posts
XmisterIS is an unknown quantity at this point
Did you write the code yourself? I can see two syntax errors,

Try this:

PHP Code:
$sql "SELECT thread_id,thread_name,comment_timestamp, COUNT(thread_name) AS p_count FROM comment WHERE comment_timestamp >= CURRENT_timestamp - INTERVAL 7 DAYS GROUP BY thread_name ORDER BY p_count DESC LIMIT 0,1";
$result mysql_query($sql$conn) or die(mysql_error());

while (
$newArray mysql_fetch_array($result))
{
  
$thread_name $newArray['thread_name'];
  
$thread_id $newArray['thread_id'];
  
$p_count $newArray['p_count'];
  echo 
"<center><a href=thread_view.php?thread_id=$thread_id'>$thread_name</a><br>Comments ($p_count)<br><br></center>";

XmisterIS is offline   Reply With Quote
Old 01-15-2012, 10:23 AM   PM User | #3
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
sorry didnt work

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 'DAYS GROUP BY thread_name ORDER BY p_count DESC LIMIT 0,1' at line 1
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:17 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I believe MySQL is like PHP. That should be +/- DAY not DAYS for the interval.
Fou-Lu is offline   Reply With Quote
Old 01-15-2012, 11:19 AM   PM User | #5
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
I have no error now, but it is not displaying anything
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:27 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by soupy1985 View Post
I have no error now, but it is not displaying anything
And using print mysql_num_rows($result); shows how many rows?
Fou-Lu is offline   Reply With Quote
Old 01-15-2012, 11:32 AM   PM User | #7
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
nothing
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:36 AM   PM User | #8
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
0 sorry
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:40 AM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by soupy1985 View Post
nothing
Then you have no records matching the criteria you have specified.
The reason is your selection of records. My assumption is you intend to find the most recent reply to the most popular topic in the last 7 days. Problem is you are selecting the most popular topics which started within the past 7 days.
The MySQL guys can help you much better than I can with this, but I'd assume if you need to find the most recent reply based on popularity you will need to get the max date as well. So try this:
Code:
SELECT thread_id, thread_name, comment_timestamp, count(thread_name) as p_count FROM comment GROUP BY thread_name HAVING MAX(comment_timestamp) >= NOW() - INTERVAL 7 DAY ORDER BY p_count DESC LIMIT 0,1;
Fou-Lu is offline   Reply With Quote
Old 01-15-2012, 11:42 AM   PM User | #10
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
what i want is the most commented thread this week.

Thanks
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:44 AM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by soupy1985 View Post
what i want is the most commented thread this week.

Thanks
And the result of the above provide?
Fou-Lu is offline   Reply With Quote
Old 01-15-2012, 11:45 AM   PM User | #12
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
doesnt work, id i take out:

Quote:
HAVING MAX(comment_timestamp) >= NOW() - INTERVAL 7 DAY
It gets the top thread of all time, so the rest of the code works, its just the timestamp part we are having probs with?
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:47 AM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is your comment_timestamp and actual datetime datatype?
Fou-Lu is offline   Reply With Quote
Old 01-15-2012, 11:50 AM   PM User | #14
soupy1985
New Coder

 
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
soupy1985 is an unknown quantity at this point
comment_timestamp is int(11) in db and it is added with time() when a commented is added. If that makes sence lol.
soupy1985 is offline   Reply With Quote
Old 01-15-2012, 11:54 AM   PM User | #15
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Then you cannot use dateinterval on it directly. You need to convert it first using FROM_UNIXTIME to convert the integer to a datetime.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:34 AM.


Advertisement
Log in to turn off these ads.