Solved

SUM values from two tables and echo in PHP

Posted on 2013-01-18
4
653 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 108

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

707 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

18 Experts available now in Live!

Get 1:1 Help Now