vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Modification Requests/Questions (Unpaid) (https://vborg.vbsupport.ru/forumdisplay.php?f=112)
-   -   Attachments per forum (https://vborg.vbsupport.ru/showthread.php?t=52839)

Boofo 05-12-2003 06:44 AM

Attachments per forum
 
Can anyone please tell what I am doing wrong or missing in this query? I am trying to get the attachments count per forum and right now it shows the same count and bytes in every forum. Here is the code:

PHP Code:

/** Count of Attachments Per Forum and Bytes **/
$attachs=$DB_site->query_first("SELECT forum.title AS forum, COUNT(attachment.attachmentid) AS count, SUM(LENGTH(attachment.filedata)) AS bytes
FROM attachment,post,thread,forum
WHERE attachment.attachmentid = post.attachmentid 
AND post.threadid=thread.threadid 
AND forum.forumid=thread.forumid
GROUP BY thread.forumid 
ORDER BY count DESC"
);

$attachs['count'] = number_format($attachs['count']);
$attachs['bytes'] = number_format($attachs['bytes']);
/** Count of Attachments Per Forum and Bytes **/ 


Chris M 05-12-2003 12:52 PM

I think your query is wrong...

I'm not sure if it is right, but try:

Code:

$attachs=$DB_site->query_first("SELECT forum.title AS forum, COUNT(attachment.attachmentid) AS count, SUM(LENGTH(attachment.filedata)) AS bytes
FROM attachment
LEFT JOIN post ON post.attachmentid=attachment.attachmentid,
LEFT JOIN thread ON thread.threadid=post.threadid,
LEFT JOIN forum ON forum.forumid=thread.forumid,
WHERE attachment.attachmentid = post.attachmentid
AND post.threadid=thread.threadid
AND forum.forumid=thread.forumid
GROUP BY thread.forumid
ORDER BY count DESC");

:)

Satan

Boofo 05-12-2003 01:08 PM

After taking the commas out after the LEFT JOINs (gave db error), I get the same results that I was getting with the other code. It shows 51 attachments, which is what I have in one forum. It shows that one very forum.

Boofo 05-13-2003 03:16 PM

Ok, we've narrowed it down to this:

SELECT SUM(attach) AS count FROM thread WHERE attach > 0 AND forumid=$forumid

Can anyone please tell me how to get the total filezise in both bytes and KB from this query?

filburt1 05-13-2003 03:17 PM

Looking into now.

Boofo 05-13-2003 03:18 PM

That was quick. Thank you, sir. ;)

filburt1 05-13-2003 03:26 PM

I got it; however it takes 3.10 sec to execute. If you're going to use it I strongly suggest you make it cache itself daily or so.
[sql]
SELECT COUNT(p.postid), SUM(LENGTH(a.filedata)), f.title
FROM post p, thread t, forum f, attachment a
WHERE a.attachmentid = p.attachmentid AND t.threadid = p.threadid
AND f.forumid = t.forumid AND p.attachmentid > 0
GROUP BY f.forumid;
[/sql]
I'm sure it can be done with JOINs, too, but I don't know how.

filburt1 05-13-2003 03:27 PM

BTW it's the SUM(LENGTH()) that's taking the most time. The query returns the number of posts with attachments, the cumulative filesize of all of those attachments, and the title of each forum, grouped by each forum.

Boofo 05-13-2003 03:36 PM

Using this shows the same attachments amount and file size in every forum. ;)

filburt1 05-13-2003 03:48 PM

Didn't for me (MySQL 4.0.12). It returned distinct forum titles and the other stats for them

Boofo 05-13-2003 03:53 PM

Ok, maybe I didn't call it right, then. Here is what I have now.

PHP Code:

$attachs=$DB_site->query_first("SELECT COUNT(p.postid) as count, SUM(LENGTH(a.filedata)) as bytes, f.title
FROM post p, thread t, forum f, attachment a
WHERE a.attachmentid = p.attachmentid AND t.threadid = p.threadid
    AND f.forumid = t.forumid AND p.attachmentid > 0
GROUP BY f.forumid"
); 

and I am calling them like this:

Quote:

$attachs['count'] = number_format($attachs['count']);
$attachs['bytes'] = number_format($attachs['bytes']);
Am I calling them wrong? And my host is using 3.54.xx version of MySQL if that makes any difference.

filburt1 05-13-2003 04:01 PM

It returns multiple rows, not one row (query() instead of query_first()).

Boofo 05-13-2003 04:11 PM

I get these errors at the top of the page when I run the following code:

Warning: Cannot use a scalar value as an array in /home/bear/public_html/forum/forumdisplay.php on line 738

Warning: Cannot use a scalar value as an array in /home/bear/public_html/forum/forumdisplay.php on line 739

PHP Code:

$attachs=$DB_site->query("SELECT COUNT(p.postid) as count, SUM(LENGTH(a.filedata)) as bytes, f.title
FROM post p, thread t, forum f, attachment a
WHERE a.attachmentid = p.attachmentid AND t.threadid = p.threadid
    AND f.forumid = t.forumid AND p.attachmentid > 0
GROUP BY f.forumid"
); 


filburt1 05-13-2003 04:24 PM

Iterate through it using $DB_site->fetch_array()...have you extensively worked with MySQL through vB before?

Boofo 05-13-2003 04:32 PM

Not a whole lot, no. Simple queries, I can usually do. ;)

I must not be getting it right or understanding it, I guess. I am still getting errors. This time it is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bear/public_html/forum/admin/db_mysql.php on line 149

filburt1 05-13-2003 08:41 PM

Iterate as such (just an example):
PHP Code:

$result $DB_site->query(...);
while (
$bits $DB_site->fetch_array($result))
{
    
// do stuff with $bits which is one row from the result



Boofo 05-13-2003 08:49 PM

We figured out what I was doing wrong with the original code in post #1. I forgot to add:

AND forum.forumid=$forumid

to the end of it. Now it returns the right info for each forum and runs fast.


All times are GMT. The time now is 10:05 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01154 seconds
  • Memory Usage 1,757KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (4)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (17)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete