Solved

Database Output Help

Posted on 2011-03-06
3
494 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 33
what do I need to host my own web sites? 13 53
mysql db 3 69
mysql query for sum() 3 28
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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.

820 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