Solved

Database Output Help

Posted on 2011-03-06
3
497 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
[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
3 Comments
 
LVL 6

Accepted Solution

by:
merwetta1 earned 500 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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