Solved

Database Output Help

Posted on 2011-03-06
3
490 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
Comment Utility
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
Comment Utility
Wow that worked, in a much easier format also. I appreciate your Help very much! Thankyou
0
 
LVL 1

Author Closing Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

11 Experts available now in Live!

Get 1:1 Help Now