General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 9th, 2002, 10:58 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
One More Quick Question

I am trying to add contents of a column, all rows, to get a total. The column is a smallint on mysql db, I JUST want total from all rows.

Example: db is for books, quantity is listed column header, I want total quantity from column.

Should I use a while...?
I can get total num_row, but not contents. Help.
__________________
bow wow!

Reply With Quote
  #2  
Old November 9th, 2002, 11:32 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Use COUNT(quantity) in your SELECT statement

PHP Code:
<? mysql_query('SELECT *,count(quantity) FROM books'); ?>


Cheers,
Joe of 4Life
__________________
Check out 4Life today!

Reply With Quote
  #3  
Old November 10th, 2002, 04:08 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Having trouble: no output

I tried your script but am having no output:

here is mine:
[PHP]$query = "SELECT *, count(quantity) FROM bookinput";
$total = mysql_query($query);
echo $total; ?>

I have already contacted db with a statement prior asking for total num_rows and now am trying to access "quantity" for total books in all row entries.
Thanks for your help, and thanks for the link to your 4life page.

Reply With Quote
  #4  
Old November 10th, 2002, 08:53 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Ok, if you already have a SELECT statement, try adding "COUNT(quantity)" to that [statement].

Good luck,
Joe of 4Life

Reply With Quote
  #5  
Old November 12th, 2002, 05:53 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Still not working

This seems so simple. Looking at this from an MSExcel point of view, all I want to do is get the Sumation of a column. I tried count(quantity) and then I tried an array approach, maybe hidden fields that gather every field from row in column "quantity" then adds all results, but no luck. I'm not getting errors, but no results:

<? @ $db = mysql_connect("localhost", "user", "password");
mysql_select_db("aquaria_books");
$query = "select * from bookinput ";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo "<strong>".$num."</strong>";
echo "<p>Total Media in DB: ";
mysql_select_db("aquaria_books");
$query = "SELECT * COUNT(quantity) FROM bookinput";
$total = mysql_query($query);
echo "<strong>".$total."</strong>"; ?>

This prints the total rows in db, but I want sumation total of specific column, the total quantity of books entered, not just the rows, since there are several copies of certain books.

Again, this seems so simple, I just am stuck.
Please, for some help here, muchos gracias.

Reply With Quote
  #6  
Old November 12th, 2002, 06:41 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Ok, try this

PHP Code:
<? $db mysql_connect('localhost''user''password');
mysql_select_db('aquaria_books');
$query 'select *, SUM(quantity) AS qsum from bookinput';
$result mysql_query($query);
$num mysql_num_rows($result);
$row mysql_fetch_array($result);
echo 
'<strong>'.$num.'</strong>';
echo 
'<p>Total Media in DB: ';
echo 
'<strong>'.$row['qsum'].'</strong>'?>

Reply With Quote
  #7  
Old November 13th, 2002, 03:21 AM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Running into trouble:

Here is what I keep getting:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

This is what I tried, after I tried Joe4JC's (very appreciated) suggestion, :

$a = array($row);
echo "sum(a) = ".array_sum($a)."\n";

Am I way off base? I think the Parse error is coming from something simple...

This has me stuck in the mud.

Reply With Quote
  #8  
Old November 13th, 2002, 03:46 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Ok, I did some testing and got it working on my server. Here is the revised code:
PHP Code:
<? $db mysql_connect('localhost''user''password');

mysql_select_db('aquaria_books');

$query 'select * from bookinput';
$query2 'select SUM(quantity) AS qsum from bookinput';
$result mysql_query($query);
$result2 mysql_query($query2);
$num mysql_num_rows($result);

$row mysql_fetch_array($result2);

echo 
'<strong>'.$num.'</strong>';

echo 
'<p>Total Media in DB: ';

echo 
'<strong>'.$row['qsum'].'</strong>'?>


Q: What line are you getting the error on?

Cheers,
Joe of 4Life

Reply With Quote
  #9  
Old November 14th, 2002, 07:15 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Driving Me Crazy!

Thanks for all your help Joe, but I just can't get this to work. I'm not going to give up, but I guess I need to try something different.

I keep getting the "T_CONSTANT_ENCAPSED_STRING" error on the first couple of lines of php code, can't tell which line yet since my Mac BBedit isn't counting the lines exactly right.

The code looks correct, no errors that I can see, just it's giving me the parse error. Glad to hear it worked on your server. The only thing I'm not telling you is that there is a session verification script at the beginning of the page, but it's closed before the html, then the script in question is inside the html.

Has anyone seen the above parse error?

Reply With Quote
  #10  
Old November 14th, 2002, 11:03 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Could you please post the other code that you are using? That is probably where the problem lies!

Good luck,
Joe of 4Life

Reply With Quote
  #11  
Old November 18th, 2002, 01:13 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Here's the code for the entire page

I pulled out some links that didn't matter with the code, to shorten it up a bit.
Everything else is just as it is on the web, except for the specifics for the database connect : )

I attached the file: countrecord.php
Attached Files
File Type: txt countrecords.txt (5.6 KB, 700 views)

Reply With Quote
  #12  
Old November 18th, 2002, 04:18 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
At the beginning of your code you misspelled "session"; that might be your problem

Cheers,
Joe of 4Life

[edit]Sorry, my computer was being dumb [/edit]

Last edited by Joe4JC : November 19th, 2002 at 03:37 PM.

Reply With Quote
  #13  
Old November 18th, 2002, 06:08 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
I did?

I couldn't find where it was misspelled, but anyway, I pulled the entire session part out and it still did the same thing, giving a parse error as before.

I am going to try something different, with you code, to see it will work. I don't see why it shouldn't.
Thanks for your help Joe.

Reply With Quote
  #14  
Old November 18th, 2002, 11:53 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 588 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Guys,
count is an aggregate function, i.e. it can't be returned with other fields -- you have to grab it seperately.

You will need 1 query for count and 1 for the other fields you want. Sorry I haven't been in the forums that much, I'm just about to release MailWorksPro v2 and a new site design for it too... today

I'll try and check in as much as i can!

Reply With Quote
  #15  
Old November 19th, 2002, 01:20 AM
markerdave markerdave is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Illinois, USA
Posts: 71 markerdave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
mytch,

Is it possible to return other field if you use "GROUP BY" in your query?
__________________
Oh I wish, I wish I hadn't killed that fish...

Reply With Quote
  #16  
Old November 19th, 2002, 01:45 AM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Getting back to what littleblackdog was saying. You want to count the rows returned from a query

then you would use this


$query = mysql_query("SELECT count(*) FROM table WHERE condition");

echo $rows = mysql_result($query, 0, 0);


If you dont know what the mysql_result function does look it up at php.net. basically it allows you to read a multi dimention mysql array. because count returns only one row, you call array 0. a bit like mysql_fetch_array; this fetches all the rows. as an array. then the second dimention of the array is 0, as count does not have a name.

you could replace

echo $rows = mysql_result($query, 0, 0);

with

$row = mysql_fetch_array($query);

echo $row[0];

they both do the same thing.

Reply With Quote
  #17  
Old November 20th, 2002, 07:15 PM
littleblackdog littleblackdog is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: Anchorage
Posts: 118 littleblackdog User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Thank you

Thanks everyone. And thanks for leaving the posts up so we can reference them at any time. I am so busy now, with a new network coming in and the regular classes asking for more lab time and help, I have little time to program.

I have a few working programs I want to share. One is a e-card ( consists of a form and about three other php pages: uses a mysql database ) and the other is code to this book input database when I get it finished. How do I go about offering these to other members?

Also, are there any articles on "include" files. I haven't used the function yet but I see the power in it. I have to copy loads of code for new users and just change their database info., but with the "include" ability, I'd just change the one page for them and use the same basic php page.

Lastly, I found a great guitar/music site using php - www.olga.net . It's great to look up and see ".php" when on a surf.

Reply With Quote
  #18  
Old November 21st, 2002, 04:08 PM
Joe4JC Joe4JC is offline
The name's Joe. Yours?
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Location: Lurking in the shadows...
Posts: 147 Joe4JC User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
To share scripts, go here. For more info on includes, go here.

Good luck,
Joe of 4Life

Last edited by Joe4JC : November 21st, 2002 at 04:15 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > One More Quick Question


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap