Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-09-2003, 04:56 AM
reefland reefland is offline
 
Join Date: Jan 2002
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Query Help

Ok, I am not completely literate when it comes to PHP but...



What I am trying to do is modify a free link system. I have everything working but am trying to add a couple of queries to pull some more info; mainly right now the total number of links per category.



First there is a link table and a category table and I am guessing that I need to work something out together. Both tables have a cat ID in them and I need to get them working together to pull the results but don't know how.



If I do this, I get the total number of categories:

PHP Code:
$countcats="SELECT COUNT(id) FROM categories WHERE visible='1'";

$countcatsresult=mysql_query($countcats);

$countcatsrow=mysql_fetch_row($countcatsresult); 


If I do this I can get the total number of links:

PHP Code:
$countquery="SELECT COUNT(id) FROM links WHERE visible='1'";

$countresult=mysql_query($countquery);

$countrow=mysql_fetch_row($countresult); 


Now I need to take each of the cateogry ID's and run a query on the link table to count the total links per cat but I can't see to get it together.



Any ideas or help would be greatly appreciated!

Thanks for helping the PHP illiterate,

Scott Z.
Reply With Quote
  #2  
Old 11-09-2003, 05:56 AM
assassingod's Avatar
assassingod assassingod is offline
 
Join Date: Jul 2002
Posts: 3,337
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

<font size="1">Just to let you know, its not a good idea to use a WHERE clause when using COUNT(col)</font>

Why dont you add a catagoryid column in the links table and then grab the info from that? I say that because you have 2 columns of the same name (id) and I don't think they'll work together
Reply With Quote
  #3  
Old 11-09-2003, 06:02 AM
reefland reefland is offline
 
Join Date: Jan 2002
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi assassingod,

There is a catid column in the links table but how would I go about getting the total number of links for each ID that is stored?

Thanks,
Scott Z.
Scott Z.
Reply With Quote
  #4  
Old 11-09-2003, 06:30 AM
assassingod's Avatar
assassingod assassingod is offline
 
Join Date: Jul 2002
Posts: 3,337
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I got it to work using something like:
PHP Code:
<?php
    $foo 
mysql_connect("localhost","root","")
        or die(
"Could not connec to DB");
 
    
$foo mysql_select_db("links",$foo)
        or die(
"Could not select DB");
 
    
$sql "SELECT * FROM links";
    
$foo mysql_query($sql)
        or die(
"Could not run query");
 
while (
$foobar mysql_fetch_array($foo))
{
    echo(
"$foobar[links] links for catid $foobar[catid]<br />");
}
?>
I think thats what you mean, using a while-loop works
Reply With Quote
  #5  
Old 11-09-2003, 07:35 AM
Lesane's Avatar
Lesane Lesane is offline
 
Join Date: Oct 2001
Location: The Netherlands
Posts: 1,149
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Try this:

PHP Code:
$get_info mysql_query("
select A.catid
     , count(AR.id) as tot 
  from categories A
left outer
  join links AR
    on A.catid = AR.catid
 where A.visible='1' 
 AND AR.visible='1'
group
    by A.catid
"
) or die("Unable to complete query: '".mysql_error()." ");
if(
mysql_num_rows($get_info)) {
while (
$infomysql_fetch_array($get_info)) {

echo 
'<br> '.$info[catid] .' has '.$info[tot].' links! ';
}
} else {
echo 
"No records found!";

Reply With Quote
  #6  
Old 11-09-2003, 11:41 PM
reefland reefland is offline
 
Join Date: Jan 2002
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Alrighty, got that one working. Now I was wondering if you could help me with another.

The table has cat_id and timestamp in it. I want to pull the row with the most recent timestamp for each cat_id. Any ideas?

Thanks,
Scott Z.
Reply With Quote
  #7  
Old 11-10-2003, 12:18 AM
reefland reefland is offline
 
Join Date: Jan 2002
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Never mind, I actually figured this one out on my own. PHP/MYSQL, it's so fun!

Thanks again for all your help,
Scott Z.
Reply With Quote
  #8  
Old 11-10-2003, 12:39 AM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by assassingod
Just to let you know, its not a good idea to use a WHERE clause when using COUNT(col)
Why? It's only brutally inefficient if you used COUNT(*).
Reply With Quote
  #9  
Old 11-10-2003, 05:52 AM
assassingod's Avatar
assassingod assassingod is offline
 
Join Date: Jul 2002
Posts: 3,337
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by filburt1
Why? It's only brutally inefficient if you used COUNT(*).
I read somewhere that it would mess up the query. I must be wrong
Reply With Quote
Reply


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. The time now is 01:55 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.04236 seconds
  • Memory Usage 2,253KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (4)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete