query select as input to next query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devikacs
    New Member
    • Jun 2007
    • 96

    query select as input to next query

    I wanted to use the select of a mysql query as the input to the next mysql query.
    The output of the first query is stored in $result.
    Can I do select * from $result.
    If not is there any other way.
  • Lumpy
    New Member
    • Oct 2007
    • 69

    #2
    Originally posted by devikacs
    I wanted to use the select of a mysql query as the input to the next mysql query.
    The output of the first query is stored in $result.
    Can I do select * from $result.
    If not is there any other way.
    To tell you for sure, I would need to see the code your working with for the first query, but it should work. Most likely you would set up your second query something like...

    [CODE=php]
    $query2 = "SELECT * from " . $result['fieldname'] . " any further query instructions here";

    [/CODE]

    Comment

    • adamalton
      New Member
      • Feb 2007
      • 93

      #3
      Yeah that should be possible...[PHP]$query1="SELECT column_name FROM my_table WHERE column_name='sa usages'";
      $result=mysql_q uery($query1);

      $search_for = mysql_result($r esult,0,'column _name');//take the first result from query1

      $query2 = "SELECT * FROM other_table WHERE other_column_na me='". $search_for ."'";
      $result2 = mysql_query($qu ery2);

      $final_result = mysql_result($r esult2,0,'other _column_name');[/PHP]

      Comment

      • Lumpy
        New Member
        • Oct 2007
        • 69

        #4
        From looking at your code, I think you got it. I read through it a couple of times and I think it should work for you.

        Comment

        • devikacs
          New Member
          • Jun 2007
          • 96

          #5
          <code=php>

          $query="select * from search";
          $result=mysql_q uery($query);
          $num=mysql_numr ows($result);
          echo $num;
          $i=0;
          while ($i < $num)
          {
          $name=mysql_res ult($result,$i, "name");
          $rollno=mysql_r esult($result,$ i,"rollno");
          echo"<p><b>$rol lno $name</b></p>";
          $i++;
          }

          $year=3;
          $query2="select * from ".$result."wher e year = ". $year;

          $res=mysql_quer y($query2);
          $num2=mysql_num rows($res);

          echo $num2;
          echo "something" ;
          $i=0;
          while ($i < $num2)
          {
          $name=mysql_res ult($res,$i,"na me");
          $rollno=mysql_r esult($res,$i," rollno");
          echo"<p><b>$rol lno $name</b></p>";
          $i++;
          }
          </code>

          This is what i have done. The first query gives proper output. But the second one doesnt print anything. The $num2 is also null. I do have values with year =3.
          The php script is not generating any error.
          What could possibly be wrong?

          Comment

          Working...