Solved

SUM values from two tables and echo in PHP

Posted on 2013-01-18
4
658 Views
Last Modified: 2013-01-18
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
0
Comment
Question by:joao_c
  • 2
  • 2
4 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38793798
Does it matter if you do one query or two?  Untested code, but probably close.

//QUERY SUM RESULT FROM FIRST TABLE (empresas);
$query_emp = "SELECT SUM(Employees_n) AS sum_e FROM empresas";
$result_emp = mysql_query($query_emp);
$row = mysql_fetch_assoc($result_emp);
$sum_e = $row['sum_e'];

//QUERY SUM RESULT FROM SECOND TABLE (anon_empresas);
$query_emp_an = "SELECT SUM(Employees_n) AS sum_a FROM anon_empresas";
$result_emp_an = mysql_query($query_emp_an);
$row = mysql_fetch_assoc($result_emp_an);
$sum_a = $row['sum_a'];

// ADD THE NUMBERS
$total = $sum_e + $sum_a;

// PUT THE VARIABLE INTO A TD-TAG
echo '<td>' . $total . '</td>';

Open in new window

0
 

Author Closing Comment

by:joao_c
ID: 38793887
Can't thank you enough. :)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38793958
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
0
 

Author Comment

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now