Link to home
Start Free TrialLog in
Avatar of joao_c
joao_cFlag for Portugal

asked on

SUM values from two tables and echo in PHP

Hello Experts.

I have one database with two tables. Need to SUM the values from those two tables and echo it in an <td>.

My current code:

//QUERY SUM RESULT FROM FIRST TABLE (empresas);

$query_emp = "SELECT Employees_n, SUM(Employees_n) FROM empresas";
$result_emp = mysql_query($query_emp);

//ECHO SUM IN A <TD>

while($row = mysql_fetch_array($result_emp)){
	echo "<td>" . $row['SUM(Employees_n)'] . "</td>";
}

////////////////////////////////////////////////////////////////////////////////////////////

//QUERY SUM RESULT FROM SECOND TABLE (anon_empresas);

$query_emp_an = "SELECT Employees_n, SUM(Employees_n) FROM anon_empresas";
$result_emp_an = mysql_query($query_emp_an);

//ECHO SUM IN A <TD>

while($row = mysql_fetch_array($result_emp_an)){
	echo "<td>" . $row['SUM(Employees_n)'] . "</td>";
}



//HOW TO ECHO IN A <TD> THE SUM OF BOTH $row['SUM(Employees_n)']??

Open in new window


What I need is to echo a <td> with the SUM of "Employees_n" from empresas and "Employees_n" from anon_empresas;

Just add the two SUMS and echo in a td;


Thanks a lot
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joao_c

ASKER

Can't thank you enough. :)
Thanks for the points and thanks for using EE.  You might want to grab a copy of this book.  It will help you get some structured learning in the art.
http://www.sitepoint.com/books/phpmysql5/

Yank's writing is very readable with great examples, and a code library you can download and modify for your own use.  Now in its fifth printing, it's been a permanent part of my professional library since Edition One.

Best regards, ~Ray
Avatar of joao_c

ASKER

I will buy that book, looks great. EE was really helpfull, and I already earn some points too. :D