vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Help needed on first try at Database Query! (https://vborg.vbsupport.ru/showthread.php?t=72983)

EvilCrow 12-16-2004 09:50 PM

Help needed on first try at Database Query!
 
I wasn't sure if this is the right section to post but I'll try here...

I'm trying to make a Database query, This will be my first one, So please be nice..

ok heres what I'm trying to do.. I have a site I'm doing for a support group. I have a userfield that members can choose a number, What I need is a database query that will add-up all the numbers and give a total...

Heres my failed stab at it..

Code:

        $result_allweight = $DB_site->query("
                SELECT * FROM " . TABLE_PREFIX . "userfield $searchclause ORDER BY field7
        ");

        $weightcount = $DB_site->num_rows($result_allweight);

I need the query to go inside the "Userfield" and count the numbers inside "field7" and give me a total for all..
btw, This code works but its only giving me the total of members not whats inside the "Field7" Table!

Davey 12-17-2004 12:34 AM

If I understand what I think you're trying to accomplish...

Try this, see if it does what you need:

PHP Code:

$result_allweight $DB_site->query_first("
   SELECT COUNT(field7) AS total,
   FROM " 
TABLE_PREFIX "userfield
   
$searchclause
"
);
/*
$result_allweight['total'] - should return the total value.
*/ 

This is an untested piece of code, I have no idea if it even works. If it doesn't, please post back stating any errors you get, I will do my best to help if I can.

If anyone notices any obvious flaws in that above code, feel free to point them out, thanks.

EvilCrow 12-17-2004 08:56 AM

I'm getting this error after using your code. :ermm:

Code:

Invalid SQL: 
  SELECT COUNT(field7) AS total,
  FROM vb3_userfield
   

mysql error: 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 'FROM vb3_userfield' at line 2

mysql error number: 1064

Let me explain better of what I'm trying to do here..
The site is for a weightloss supprot group and during registering they have a userfield option (field7) where they can put in how much weight they lost. What I'd like is a total of all the members weight loss added up and shown on the forum home page. The code I posted in the first post works somewhat as its returning the amount of tables (members) and its not adding whats in the "Field7" table.. :disappointed:

Brad 12-17-2004 09:45 AM

What are you using for the $searchclause var?

EvilCrow 12-17-2004 09:55 AM

Quote:

Originally Posted by Brad.loo
What are you using for the $searchclause var?

erm... I guess nothing. :disappointed:
Like I said this is my FIRST try at php/mysql coding!

dimopoulos 12-17-2004 01:59 PM

Get rid of the comma after the COUNT(field7) AS total, <-

That should sort it out

EvilCrow 12-17-2004 03:03 PM

Quote:

Originally Posted by dimopoulos
Get rid of the comma after the COUNT(field7) AS total, <-

That should sort it out

Did that and now I don't get any errors but insted of posting a total its only posting "Array"?? :disappointed:

EvilCrow 12-19-2004 11:23 AM

Can someone help plz??? :confused:

Paul M 12-19-2004 01:15 PM

Please post the code you are using to display the result.

EvilCrow 12-20-2004 11:29 AM

1 Attachment(s)
Quote:

Originally Posted by Paul M
Please post the code you are using to display the result.


Heres the code I'm using.. its give me a "Array" as the result. (see attached image)

Code:

$result_allweight = $DB_site->query_first("
  SELECT COUNT(field7) AS total
  FROM " . TABLE_PREFIX . "userfield
");
/*
$result_allweight['total'] - should return the total value.
*/



All times are GMT. The time now is 03:09 AM.

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.01110 seconds
  • Memory Usage 1,739KB
  • 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
  • (3)bbcode_code_printable
  • (1)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete