Nested SQL Statements

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Relatively new to PHP.  Having an issue trying to nest sql statements. 
Basically I am trying to pull a variable from SQL1, Pass it as a 
Variable/Bind or Parm to SQL2 and then Go back to SQL1 and pull the next 
value and pass to SQL2 again for processing.

It seems like the SQL2 is getting stuck on the first value passed by SQL1. 
Like SQL2 does not rebuild with the new variable.

In my example I am testing reading all my tables and reporting their 
definitions.  I have other uses for this technique if PHP and MySQL support.


<?

require_once ('mysql_connect.php');


   $result1 = mysql_query('show tables',$dbc);


   if ($myrow1 = mysql_fetch_array($result1))
    {

      // display list if there are records to display

  $tmptablename = sprintf("describe {$myrow1[0]}");


       do {


  $result2 = mysql_query($tmptablename,$dbc);

  echo "Table: {$myrow1[0]}";

        // Create page headers

   echo "<table border=\"1\" cellspacing=\"1\" width=\"80%\" 
id=\"{$myrow1[0]}\">";
   echo "<tr>";
   echo "<td width=\"20%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Field</td>";
   echo "<td width=\"20%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Type</td>";
   echo "<td width=\"5%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Null</td>";
   echo "<td width=\"10%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Key</td>";
   echo "<td width=\"20%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Default</td>";
   echo "<td width=\"25%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Extra</td>";
   echo "</tr>";

       if ($myrow2 = mysql_fetch_array($result2))
           {

          // display list if there are records to display

           do {

          echo "<tr>";
          echo "<td width=\"20%\">";
       if ("{$myrow2['Field']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Field']}";
       }

      echo "</td>\n";

          echo "<td width=\"20%\">";
       if ("{$myrow2['Type']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Type']}";
       }

      echo "</td>\n";


          echo "<td width=\"5%\">";
       if ("{$myrow2['Null']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Null']}";
       }

      echo "</td>\n";

          echo "<td width=\"10%\">";

       if ("{$myrow2['Key']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Key']}";
       }

      echo "</td>\n";

          echo "<td width=\"20%\">";

       if ("{$myrow2['Default']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Default']}";
       }

      echo "</td>\n";

          echo "<td width=\"25%\">";
       if ("{$myrow2['Extra']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Extra']}";
       }
      echo "</td>\n";
          echo "</td>\n";
          echo "</tr>\n";

               } while ($myrow2 = mysql_fetch_array($result2));

             echo "</table>";
         echo "<br>";

     $myrow2 = Null;

      mysql_free_result(result2);

          } else {

           // no records to display

            echo "Sorry, no records were found!";}

    } while ($myrow1 = mysql_fetch_array($result1));

 }

     else {

           echo "No Tables";

  }
    mysql_close();

include ('footer.php');
?> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux