Friday, June 8, 2012

Joining 3 Tables php mysql

Joining 3 Tables php mysql

This tutorial in addition to the product and customer table we use the table orders.Joining scrips are as follows.

<?php
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql ="SELECT product.product_name, customer.customer_name,orders.date FROM product
    INNER JOIN  customer ON customer.product_id=product.product_id INNER JOIN orders ON customer.customer_id=orders.customer_id";
     $result=mysql_query($sql);
?>
<table  align="center"  bgcolor="#15475c">
<tr bgcolor="#ddeef5">
<td align="center" ><strong>Product Name</strong></td>
<td align="center" "><strong>Customer Name</strong></td>
<td align="center" "><strong>Ordered Date</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr bgcolor="#d0e7f1">
<td><?php echo $rows['product_name']; ?></td>
<td><?php echo $rows['customer_name']; ?></td>
<td><?php echo $rows['date']; ?></td>
</tr>
<?php
}
?>
</table>

Bellow is the Final table display.

Product Name Customer Name Ordered Date
3D Max 9 Bayker 2012-06-08
Win 2007 Jones 2012-06-06
Photoshop Cs5 Harfer 2012-06-01
Maya 10 Lopez 2012-06-03
Ilustrator cs5 Taylor 2012-05-08
Next

No comments:

Post a Comment