[PHP-DB] Count database-values
Hi everybody, I've got a little problem, which I can't solve. It is as follow: In my database I made a table with different fields. One of the fields is named 'bedrag' and contains a numeric value like 15.47 or 78.16 and so on. If I want a value of the table I use in most cases the following code: "; } ?> In this case I receive a list like: 15.47 78.16 and so on... So far no problems, but I want to count all these values. I tried with SUM but with the following code it doesn't work: $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; any suggestions how to do? I want to store it in a variabele ($total) so I can echo it. (E.g.: $total = 93.63 in this case) Thanx in advance, Robert van der Mast
Re: [PHP-DB] Count database-values
> So far no problems, but I want to count all these values. > I tried with SUM but with the following code it doesn't work: > > $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; > > any suggestions how to do? I want to store it in a variabele ($total) > so I can echo it. (E.g.: $total = 93.63 in this case) If you want the total, you need to do something like the following: $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; $result = mysql_query( $query ); $total = mysql_fetch_array( $result ); $total = $total['Total']; You had forgotten to pull the data out of your result set. It's a common mistake. :) Edward Dudlik Becoming Digital www.becomingdigital.com - Original Message - From: "Robert Wanadoo" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, 09 June, 2003 11:40 Subject: [PHP-DB] Count database-values Hi everybody, I've got a little problem, which I can't solve. It is as follow: In my database I made a table with different fields. One of the fields is named 'bedrag' and contains a numeric value like 15.47 or 78.16 and so on. If I want a value of the table I use in most cases the following code: "; } ?> In this case I receive a list like: 15.47 78.16 and so on... So far no problems, but I want to count all these values. I tried with SUM but with the following code it doesn't work: $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; any suggestions how to do? I want to store it in a variabele ($total) so I can echo it. (E.g.: $total = 93.63 in this case) Thanx in advance, Robert van der Mast -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Count database-values
I screwed up my own code. Silly me. It should read: $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; $result = mysql_query( $query ); while ( $total = mysql_fetch_array( $result ) ) { $total = $total['Total']; } Edward Dudlik Becoming Digital www.becomingdigital.com - Original Message - From: "Becoming Digital" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, 10 June, 2003 06:43 Subject: Re: [PHP-DB] Count database-values > So far no problems, but I want to count all these values. > I tried with SUM but with the following code it doesn't work: > > $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; > > any suggestions how to do? I want to store it in a variabele ($total) > so I can echo it. (E.g.: $total = 93.63 in this case) If you want the total, you need to do something like the following: $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; $result = mysql_query( $query ); $total = mysql_fetch_array( $result ); $total = $total['Total']; You had forgotten to pull the data out of your result set. It's a common mistake. :) Edward Dudlik Becoming Digital www.becomingdigital.com - Original Message - From: "Robert Wanadoo" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, 09 June, 2003 11:40 Subject: [PHP-DB] Count database-values Hi everybody, I've got a little problem, which I can't solve. It is as follow: In my database I made a table with different fields. One of the fields is named 'bedrag' and contains a numeric value like 15.47 or 78.16 and so on. If I want a value of the table I use in most cases the following code: "; } ?> In this case I receive a list like: 15.47 78.16 and so on... So far no problems, but I want to count all these values. I tried with SUM but with the following code it doesn't work: $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE posneg = 'af'"; any suggestions how to do? I want to store it in a variabele ($total) so I can echo it. (E.g.: $total = 93.63 in this case) Thanx in advance, Robert van der Mast -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Count database-values
> -Original Message- > From: Becoming Digital [mailto:[EMAIL PROTECTED] > Sent: 10 June 2003 12:15 > > I screwed up my own code. Silly me. It should read: Surely your first attempt is the right one? There's only ever going to be 1 Total, so why waste a while loop trying to read more than one result row? > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > posneg = 'af'"; > $result = mysql_query( $query ); > while ( $total = mysql_fetch_array( $result ) ) > { > $total = $total['Total']; > } > > Edward Dudlik > Becoming Digital > www.becomingdigital.com > > > - Original Message - > From: "Becoming Digital" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Tuesday, 10 June, 2003 06:43 > Subject: Re: [PHP-DB] Count database-values > > > > So far no problems, but I want to count all these values. > > I tried with SUM but with the following code it doesn't work: > > > > $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE > posneg = 'af'"; > > > > any suggestions how to do? I want to store it in a > variabele ($total) > > so I can echo it. (E.g.: $total = 93.63 in this case) > > If you want the total, you need to do something like the following: > > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > posneg = 'af'"; > $result = mysql_query( $query ); > $total = mysql_fetch_array( $result ); > $total = $total['Total']; > Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Count database-values
Unfortunately, I can't, despite my best efforts, get the data to display unless it's put inside a loop. If anyone can tell me how to, I'd just for joy. Edward Dudlik Becoming Digital www.becomingdigital.com - Original Message - From: "Ford, Mike [LSS]" <[EMAIL PROTECTED]> To: "'Becoming Digital'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Tuesday, 10 June, 2003 11:54 Subject: RE: [PHP-DB] Count database-values > -Original Message- > From: Becoming Digital [mailto:[EMAIL PROTECTED] > Sent: 10 June 2003 12:15 > > I screwed up my own code. Silly me. It should read: Surely your first attempt is the right one? There's only ever going to be 1 Total, so why waste a while loop trying to read more than one result row? > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > posneg = 'af'"; > $result = mysql_query( $query ); > while ( $total = mysql_fetch_array( $result ) ) > { > $total = $total['Total']; > } > > Edward Dudlik > Becoming Digital > www.becomingdigital.com > > > ----- Original Message - > From: "Becoming Digital" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Tuesday, 10 June, 2003 06:43 > Subject: Re: [PHP-DB] Count database-values > > > > So far no problems, but I want to count all these values. > > I tried with SUM but with the following code it doesn't work: > > > > $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE > posneg = 'af'"; > > > > any suggestions how to do? I want to store it in a > variabele ($total) > > so I can echo it. (E.g.: $total = 93.63 in this case) > > If you want the total, you need to do something like the following: > > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > posneg = 'af'"; > $result = mysql_query( $query ); > $total = mysql_fetch_array( $result ); > $total = $total['Total']; > Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Count database-values
Edward, Have you considered trying mysql_result()? It appears that your query is going to always return a single piece of data: Total. So you essentially have an array with only a single element. If you did this: $total = mysql_result($result,0); You'll get the value of the item at index 0 (the first position in an array) of the $result array stored in the $total variable. Details are here: http://us3.php.net/manual/en/function.mysql-result.php The page also makes reference to other "high-performance" options, but since you're only grabbing a single item and if you use the index number instead of the column name as the documentation recommends, the performance should be just fine. Hope this helps. Rich > -Original Message- > From: Becoming Digital [mailto:[EMAIL PROTECTED] > Sent: Tuesday, June 10, 2003 12:50 PM > To: [EMAIL PROTECTED] > Subject: Re: [PHP-DB] Count database-values > > > Unfortunately, I can't, despite my best efforts, get the data > to display unless > it's put inside a loop. If anyone can tell me how to, I'd > just for joy. > > Edward Dudlik > Becoming Digital > www.becomingdigital.com > > > - Original Message - > From: "Ford, Mike [LSS]" <[EMAIL PROTECTED]> > To: "'Becoming Digital'" <[EMAIL PROTECTED]>; > <[EMAIL PROTECTED]> > Sent: Tuesday, 10 June, 2003 11:54 > Subject: RE: [PHP-DB] Count database-values > > > > -Original Message- > > From: Becoming Digital [mailto:[EMAIL PROTECTED] > > Sent: 10 June 2003 12:15 > > > > I screwed up my own code. Silly me. It should read: > > Surely your first attempt is the right one? There's only > ever going to be 1 > Total, so why waste a while loop trying to read more than one > result row? > > > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > > posneg = 'af'"; > > $result = mysql_query( $query ); > > while ( $total = mysql_fetch_array( $result ) ) > > { > > $total = $total['Total']; > > } > > > > Edward Dudlik > > Becoming Digital > > www.becomingdigital.com > > > > > > - Original Message - > > From: "Becoming Digital" <[EMAIL PROTECTED]> > > To: <[EMAIL PROTECTED]> > > Sent: Tuesday, 10 June, 2003 06:43 > > Subject: Re: [PHP-DB] Count database-values > > > > > > > So far no problems, but I want to count all these values. > > > I tried with SUM but with the following code it doesn't work: > > > > > > $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE > > posneg = 'af'"; > > > > > > any suggestions how to do? I want to store it in a > > variabele ($total) > > > so I can echo it. (E.g.: $total = 93.63 in this case) > > > > If you want the total, you need to do something like the following: > > > > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > > posneg = 'af'"; > > $result = mysql_query( $query ); > > $total = mysql_fetch_array( $result ); > > $total = $total['Total']; > > > > Cheers! > > Mike > > - > Mike Ford, Electronic Information Services Adviser, > Learning Support Services, Learning & Information Services, > JG125, James Graham Building, Leeds Metropolitan University, > Beckett Park, LEEDS, LS6 3QS, United Kingdom > Email: [EMAIL PROTECTED] > Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Count database-values
I love when things go completely over my head for no apparent reason. I have an incredible talent for missing the obvious. Edward Dudlik Becoming Digital www.becomingdigital.com - Original Message - From: "Hutchins, Richard" <[EMAIL PROTECTED]> To: "'Becoming Digital'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Tuesday, 10 June, 2003 13:14 Subject: RE: [PHP-DB] Count database-values Edward, Have you considered trying mysql_result()? It appears that your query is going to always return a single piece of data: Total. So you essentially have an array with only a single element. If you did this: $total = mysql_result($result,0); You'll get the value of the item at index 0 (the first position in an array) of the $result array stored in the $total variable. Details are here: http://us3.php.net/manual/en/function.mysql-result.php The page also makes reference to other "high-performance" options, but since you're only grabbing a single item and if you use the index number instead of the column name as the documentation recommends, the performance should be just fine. Hope this helps. Rich > -Original Message- > From: Becoming Digital [mailto:[EMAIL PROTECTED] > Sent: Tuesday, June 10, 2003 12:50 PM > To: [EMAIL PROTECTED] > Subject: Re: [PHP-DB] Count database-values > > > Unfortunately, I can't, despite my best efforts, get the data > to display unless > it's put inside a loop. If anyone can tell me how to, I'd > just for joy. > > Edward Dudlik > Becoming Digital > www.becomingdigital.com > > > - Original Message - > From: "Ford, Mike [LSS]" <[EMAIL PROTECTED]> > To: "'Becoming Digital'" <[EMAIL PROTECTED]>; > <[EMAIL PROTECTED]> > Sent: Tuesday, 10 June, 2003 11:54 > Subject: RE: [PHP-DB] Count database-values > > > > -Original Message- > > From: Becoming Digital [mailto:[EMAIL PROTECTED] > > Sent: 10 June 2003 12:15 > > > > I screwed up my own code. Silly me. It should read: > > Surely your first attempt is the right one? There's only > ever going to be 1 > Total, so why waste a while loop trying to read more than one > result row? > > > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > > posneg = 'af'"; > > $result = mysql_query( $query ); > > while ( $total = mysql_fetch_array( $result ) ) > > { > > $total = $total['Total']; > > } > > > > Edward Dudlik > > Becoming Digital > > www.becomingdigital.com > > > > > > - Original Message - > > From: "Becoming Digital" <[EMAIL PROTECTED]> > > To: <[EMAIL PROTECTED]> > > Sent: Tuesday, 10 June, 2003 06:43 > > Subject: Re: [PHP-DB] Count database-values > > > > > > > So far no problems, but I want to count all these values. > > > I tried with SUM but with the following code it doesn't work: > > > > > > $squery = "SELECT SUM(bedrag) AS Total FROM finance WHERE > > posneg = 'af'"; > > > > > > any suggestions how to do? I want to store it in a > > variabele ($total) > > > so I can echo it. (E.g.: $total = 93.63 in this case) > > > > If you want the total, you need to do something like the following: > > > > $query = "SELECT SUM(bedrag) AS Total FROM finance WHERE > > posneg = 'af'"; > > $result = mysql_query( $query ); > > $total = mysql_fetch_array( $result ); > > $total = $total['Total']; > > > > Cheers! > > Mike > > - > Mike Ford, Electronic Information Services Adviser, > Learning Support Services, Learning & Information Services, > JG125, James Graham Building, Leeds Metropolitan University, > Beckett Park, LEEDS, LS6 3QS, United Kingdom > Email: [EMAIL PROTECTED] > Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php