0

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

2
Contributors
5
Replies
8
Views
7 Years
Discussion Span
Last Post by d5e5
0

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

0
<?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'.

0

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

0

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.

Edited by mpc123: n/a

0

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'";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.