Put multiple querys in array

Discussion in 'PHP' started by lammspillning, Jan 26, 2010.

  1. #1
    How would I go on to do these mysql_querys

    mysql_query("SELECT * FROM my_table_1");
    mysql_query("SELECT * FROM my_table_2");
    mysql_query("SELECT * FROM my_table_3");
    mysql_query("SELECT * FROM my_table_4");

    ..and put all the results in a array and echo that array..?
     
    lammspillning, Jan 26, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $tables = array('my_table_1', 'my_table_2', 'my_table_3', 'my_table_4');
    $result_array = array();
    foreach ($tables as $table)
    {
       $sql = "select * from {$table}";
       $st = mysql_query($sql) or die (mysql_error());
       while ($row = mysql_fetch_row($st))
          $result_array[] = $row;
    }
    echo "<table border='1'>";
    foreach ($result_array as $row)
       echo "<tr><td>" . join("</td><td>", $row) . "</td></tr>";
    echo "</table>";
    PHP:
     
    SmallPotatoes, Jan 26, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    jestep, Jan 26, 2010 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If the tables are large, using UNION will take up more RAM because the result set will be larger. Although I could have used a mysql_free_result() in the loop...

    The original request seems a bit wonky to me, in almost 15 years of using PHP I've never seen a reason to do that.
     
    SmallPotatoes, Jan 26, 2010 IP