Search code examples
phpmysqlmysqli-multi-query

Php multiple mySQL query


is it possible to retrieve an array of data from more than one table within one query? For example , I am getting an array from table1, but I want to retrieve data from several other tables too:

<?php


   $con = mysql_connect($hostname,$username, $password);
   if (!$con)
     {
     die('Could not connect: ' . mysql_error());
   }
   mysql_select_db($dbname, $con);

   $today = date('Y-m-d H:i:s', time());
   $today1DayAgo = date('Y-m-d H:i:s', strtotime("$today -1 day"));
   $query = "SELECT * FROM table1 WHERE omtr_date BETWEEN '$today1DayAgo' AND '$today'";

   $result = mysql_query($query);

   while($row = mysql_fetch_array($result)){
       echo $row["omtr_page_view"]);
   }

   mysql_close($con);

  ?>

Thanks


Solution

  • Use Mysql Joins.

    Here is an example:

    <?php
    // Make a MySQL Connection
    
    // Construct our join query
    $query  = "SELECT family.Position, food.Meal ";
    $query .= "FROM family INNER JOIN food ";
    $query .= "WHERE family.Position = food.Position";
    
    //Execute query  
    $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 />";
    }
    ?>
    

    More examples of mysql joins are listed here: http://phpweby.com/tutorials/mysql/32