Hi all you guru's.

Who could crack this once for me.

I want to add the totals of my records posted to a mysql database table. I can achieve this by using the += method, but I have to have it in a repeated region (dreamweaver). I know there is certainly a shorter method, please, anyone,, assist me in this.

Here is an example of the database table:

CREATE TABLE IF NOT EXISTS `sample` (
  `pmtID` int(11) NOT NULL auto_increment,
  `AccNo` varchar(250) NOT NULL default '',
  `Description` varchar(250) NOT NULL default '',
  `Amount` decimal(9,2) default '0.00',
  PRIMARY KEY  (`pmtID`)
);

So after posting several entries to the database table above, I'd like to have to totals of all the entries of the Amount field.

Then, another Q ;-)

How do I count how many entries my table has? I don't want to simply read the ID recod, I'd like to have a method to know how many entries I have posted.

Please guys, and thanx for all the previous support!!!!!!

Recommended Answers

All 6 Replies

Anyone who could crack this one for me, PLEASE... :-) I'm stuck without it.

Total:

select sum(`Amount`) as total from `sample`;

Total Records:

select count(`pmtID`) as total from `sample`;

Total:

select sum(`Amount`) as total from `sample`;

Total Records:

select count(`pmtID`) as total from `sample`;

Hey.

Thanx for the reply.

I'm really very basic in PHP. I have tried to implement/use the above example, but could'nt get it to work. I know that example is correct, I'm simply just to dumb to see te logic.

Please assist.

Below is what I have done to test it;

this is my database table

CREATE TABLE IF NOT EXISTS `sample` (
  `pmtID` int(11) NOT NULL auto_increment,
  `AccNo` varchar(250) NOT NULL default '',
  `Description` varchar(250) NOT NULL default '',
  `Amount` decimal(9,2) default '0.00',
  PRIMARY KEY  (`pmtID`)
);

Then my "coding"

<?php require_once('Connections/testdb.php'); ?>
<?php
mysql_select_db($database_staydb, $staydb);
$query_test = "SELECT count(`pmtID`) AS total FROM `sample`;";
$test = mysql_query($query_test, $staydb) or die(mysql_error());
$row_test = mysql_fetch_assoc($test);
$totalRows_test = mysql_num_rows($test);

echo $row_test;?>

<?php
mysql_free_result($test);
?>

Just to add, I have 11 entires in this table. If I say:

echo $row_test;

Then I get the result "Array"

If I say:

echo $test;

Then I get the result: Resource id #4

replace:
echo $row_test;?>

with
echo $row_test;?>

Wow, thanx.

Do you have a page where I can donate?

or pay ;-) lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.