[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SUM values from two tables and echo in PHP

Posted on 2013-01-18
4
Medium Priority
?
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remaiā€¦
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

650 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