Hi im looking to echo a result of a query code i have at the moment is connections +

<?php
$result = mysql_query("SELECT SUM(tablefield) FROM table") 
or die(mysql_error());

got no idea if this is right, i seem to have got really mixed up with this.

Can anybody help please

sorry copied totally the wrong thing, i have this code

connx +

$query = "SELECT SUM(Profit) FROM table";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
	echo "Total ". $row['SUM(profit)'];
}
?>

I get the word total on the html page but no actaul figure total of the sum, can somebody help please

<?php
require('includes/init_mysql.php');
$con = mysql_connect($db_host,$db_user,$db_pass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("daniweb", $con);

$sql = "SELECT SUM(ct) FROM `test2`";

$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($result)){
	echo "Total ". $row['SUM(ct)'];
}

mysql_close($con);
?>

This displays Total 6 on my browser. My code is the same as yours except for the name of the column ('ct' instead of 'Profit') and table ('test2' instead of 'table'). Are you sure your table really has a column named 'Profit' and does it have data? Is your table really named 'table'. I don't know if naming your table 'table' is causing a problem but in principle it's not good practice to name a table after an SQL reserved word such as 'table'.

thanks for your help , it now works. Thanks also for the advice. I will mark as solved

Also how would i do this by date also, eg if i only want to show the total profit between 2 certain dates what the code be?

There is a date field in the database.

Also how would i do this by date also, eg if i only want to show the total profit between 2 certain dates what the code be?

There is a date field in the database.

If your date field is called 'dt', for example, you can modify your query as follows:

$query = "SELECT SUM(Profit) FROM your_tbl WHERE dt BETWEEN '2005-01-01' AND '2005-12-31'";
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.