How we can fetch data from two tables on a same place in php?

Discussion in 'PHP' started by jagjeetsingh, Feb 24, 2010.

  1. #1
    Hello friends
    How we can fetch data from two tables on a same place in php?
    for e.g.

    <?php
    		 $slct=mysql_query("select * from phn") or die(mysql_error());
    			while($ftch=mysql_fetch_array($slct))
    			{	
    			 ?>
    
    <?php
    		 	$slct=mysql_query("select * from mv" )or die(mysql_error());
    			while($ftch=mysql_fetch_array($slct))
    			{
    			 ?>
    PHP:
    i want to show both tables data on a same place.
    Thanks in advance.
     
    jagjeetsingh, Feb 24, 2010 IP
  2. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can simply use join.
    Or there are more way to do it.
     
    Om ji Kesharwani, Feb 24, 2010 IP
  3. jagjeetsingh

    jagjeetsingh Peon

    Messages:
    104
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    please can u explain little more with an ex.
    thank you
     
    jagjeetsingh, Feb 24, 2010 IP
  4. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    // Make a MySQL Connection
    // Construct our join query
    $query = "SELECT family.Position, food.Meal ".
    "FROM family LEFT JOIN food ".
    "ON family.Position = food.Position";

    $result = mysql_query($query) or die(mysql_error());


    // Print out the contents of each row into a table
    while($row = mysql_fetch_array($result)){
    echo $row['Position']. " - ". $row['Meal'];
    echo "<br />";
    }
    ?>

    This is left join example. Left join fetch all the record for left table, in this case - family table and fetch matching record for right table.
    Vice-versa for right join
     
    Om ji Kesharwani, Feb 24, 2010 IP