?
Solved

Database Output Help

Posted on 2011-03-06
3
Medium Priority
?
503 Views
Last Modified: 2013-12-13
Im Writing a Function that Searches 1 database called financial that selects all "id" numbers where the value of "name = income" then I take that result to run another query to search my banking database to select all my transactions with the account_id that = all the ids from the last query, but then I want to display "Income" and the sum amount. The problem is I cant do a join table method cause each table is in a different database. I hope that makes sense. Anybody have a simpler way to make this work?? here is my code below, Thanks Experts!!
function test()
{

echo '<table border=1>';

include ($_SERVER['DOCUMENT_ROOT'] . '/onlineoffice/config/config.php');

mysql_pconnect($host, $user, $pass) or die ('Unable To Connect To Database!');

mysql_select_db('finanical_schema') or die ('Unable To Select Database!');

$query = "SELECT id, name from shema_names WHERE type = 'Income' GROUP BY id";

$result = mysql_query($query) or die ('Error in Selection query: $query. ' . mysql_error());

while ($row = mysql_fetch_array($result))

	{
	
	$data(
		"id" => $row[0];
		"name" => $row[1];
	)
	
	}

	foreach ($data as $id)

		{

		mysql_select_db('b_accounts') or die ('Cant Select Bank Database!');

		$sql = "SELECT account_id, sum(if(type='Withdrawl', -amount,amount)) FROM Banking WHERE account_id  IN  ('$id') GROUP BY account_id";

		$compute = mysql_query($sql) or die ('Error in Bank Query: $sql. ' . mysql_error());
			
			while ($display = mysql_fetch_array($compute, MYSQL_NUM))

				{
				
				echo '<tr><td>'. $id['name'] . '<td><div align="right">' . $display[1] . '</div></td></tr>';
					
				}
		}

		echo '</table>';

mysql_close();

Open in new window

0
Comment
Question by:Easyrider43
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
merwetta1 earned 2000 total points
ID: 35048179
You should be able to do one query on both databases like this:


SELECT id, name,
account_id, sum(if(type='Withdrawl', -amount,amount))
from finanical_schema.shema_names, b_accounts.Banking
WHERE type = 'Income'
and account_id = id
GROUP BY id

If you have conflicting field names, you might have to put "<database name>.<table_name>." in front of field names (e.g. WHERE b_accounts.Banking.type = 'Income' )
0
 
LVL 1

Author Comment

by:Easyrider43
ID: 35048257
Wow that worked, in a much easier format also. I appreciate your Help very much! Thankyou
0
 
LVL 1

Author Closing Comment

by:Easyrider43
ID: 35048262
He made my query more simple and faster and less complex. Im a very happy guy now. Thankyou!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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