How do I display data from multiple tables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

How do I display data from multiple tables?

Post by michlcamp »

I know how to query two tables for the data I'm looking for, but I don't know how to display it. One table has customer information, the other table has orders placed by the customers.

I'm using this query:
SELECT customers.realname, orders.product FROM customers, orders WHERE customers.realname=orders.realname;

now do I write a WHILE statement?
How to display the data on a page?

looing to display it like this:

Customer:Product
Customer:Product
Customer:Product

All help much appreciated!
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

There is actually nothing special to this as long as you just think about the array you will get from the server. Just don't try figuring out what the mySQL server is going through as that can be mind boggling in a lot of cases. :)

Code: Select all

$mysql = "SELECT customers.realname, orders.product FROM customers, orders WHERE customers.realname=orders.realname";

$results = mysql_query( $mysql, $db ) 
      or die ('Cannot execute mySQL query because: ' . mysql_error());

while($row = mysql_fetch_array($results)) {
	print("$row[0]: $row[1]<br>\n");
}

mysql_free_result($results);
Post Reply