|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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! |
|
#2
|
|||
|
|||
|
Use COUNT(quantity) in your SELECT statement
![]() PHP Code:
Cheers, Joe of 4Life ![]()
__________________
Check out 4Life today! |
|
#3
|
|||
|
|||
|
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. ![]() |
|
#4
|
|||
|
|||
|
Ok, if you already have a SELECT statement, try adding "COUNT(quantity)" to that [statement].
Good luck, Joe of 4Life ![]() |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
Ok, try this
PHP Code:
|
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
Ok, I did some testing and got it working on my server. Here is the revised code:
PHP Code:
Q: What line are you getting the error on? Cheers, Joe of 4Life ![]() |
|
#9
|
|||
|
|||
|
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? |
|
#10
|
|||
|
|||
|
Could you please post the other code that you are using? That is probably where the problem lies!
![]() Good luck, Joe of 4Life ![]() |
|
#11
|
|||
|
|||
|
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 |
|
#12
|
|||
|
|||
|
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 02:37 PM. |
|
#13
|
|||
|
|||
|
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. |
|
#14
|
|||
|
|||
|
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! |
|
#15
|
|||
|
|||
|
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... |
|
#16
|
|||
|
|||
|
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. |