Is this Possible - PHP/MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phillip T. Murphy

    Is this Possible - PHP/MySQL

    Well, after half pulling my hair out messing with this, I am thinking it is
    not possible. I did research and found references to "sub-queries" not
    possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I am
    breaking that rule or not.

    Psuedo code of what I am trying to do...

    <?php

    $sql = "select * from table1";
    $db_result = mysql_query($sq l);

    while ($row = mysql_fetch_arr ay($db_result)) {
    $table_key = $row("table_key ");


    $sql2 = "select * from table2 where tabel_key='$tab le_key'";
    $db_result2 = mysql_query($sq l2);
    $row2 = mysql_fetch_arr ay($db_result2) ;

    if record exists in table2
    do something with table 2;
    else
    do something with table 1;
    }
    ?>

    The second query seems to always return an empty array. Is this nested
    query allowed in PHP/MySQL?

    Thanks in advance for any help,

    PTM


  • Geoff Berrow

    #2
    Re: Is this Possible - PHP/MySQL

    I noticed that Message-ID: <bg8kd.753$z3.2 79@bignews5.bel lsouth.net>
    from Phillip T. Murphy contained the following:
    [color=blue]
    >The second query seems to always return an empty array.[/color]

    tabel_key?
    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Michael Fesser

      #3
      Re: Is this Possible - PHP/MySQL

      .oO(Phillip T. Murphy)
      [color=blue]
      >Well, after half pulling my hair out messing with this, I am thinking it is
      >not possible. I did research and found references to "sub-queries" not
      >possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I am
      >breaking that rule or not.[/color]

      A subquery (or subselect) is a SELECT statement nested inside another:

      SELECT something FROM somewhere WHERE foo = (SELECT somethingelse ...)

      Subqueries are supported as of MySQL 4.1.
      [color=blue]
      >The second query seems to always return an empty array. Is this nested
      >query allowed in PHP/MySQL?[/color]

      No problem with that.

      But you have no error checking in your code, you won't notice when a
      query fails. Check if mysql_query() returns a resource ID, if not have a
      look at mysql_error() to see if MySQL encountered a problem.


      Returns the text of the error message from previous MySQL operation


      Micha

      Comment

      • Phillip T. Murphy

        #4
        Re: Is this Possible - PHP/MySQL


        "Michael Fesser" <netizen@gmx.ne t> wrote in message
        news:cp62p01egp cntcr210fu10g3b bm0igjbf1@4ax.c om...[color=blue]
        > .oO(Phillip T. Murphy)
        >[color=green]
        >>Well, after half pulling my hair out messing with this, I am thinking it
        >>is
        >>not possible. I did research and found references to "sub-queries" not
        >>possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I
        >>am
        >>breaking that rule or not.[/color]
        >
        > A subquery (or subselect) is a SELECT statement nested inside another:
        >
        > SELECT something FROM somewhere WHERE foo = (SELECT somethingelse ...)
        >
        > Subqueries are supported as of MySQL 4.1.
        >[color=green]
        >>The second query seems to always return an empty array. Is this nested
        >>query allowed in PHP/MySQL?[/color]
        >
        > No problem with that.
        >
        > But you have no error checking in your code, you won't notice when a
        > query fails. Check if mysql_query() returns a resource ID, if not have a
        > look at mysql_error() to see if MySQL encountered a problem.
        >
        > http://www.php.net/mysql_query
        > http://www.php.net/mysql_error
        >
        > Micha[/color]

        Thanks for the quick reply. in the interest of space, I cut that out. I
        have a function defined that replaces the mysql_query() call and does error
        checking.

        At least I know what I am trying to do is possible now. I am starting over.
        I would like to be able to just use a LEFT JOIN, but the second table needs
        a where clause on a field that does not exist in the first table, so too
        many records are elimiated.

        I will keep trying. Thanks for the input.


        Comment

        • kingofkolt

          #5
          Re: Is this Possible - PHP/MySQL

          "Phillip T. Murphy" <pStPmAuMrphy@b ellsouth.net> wrote in message
          news:bg8kd.753$ z3.279@bignews5 .bellsouth.net. ..[color=blue]
          > Well, after half pulling my hair out messing with this, I am thinking it[/color]
          is[color=blue]
          > not possible. I did research and found references to "sub-queries" not
          > possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I[/color]
          am[color=blue]
          > breaking that rule or not.
          >
          > Psuedo code of what I am trying to do...
          >
          > <?php
          >
          > $sql = "select * from table1";
          > $db_result = mysql_query($sq l);
          >
          > while ($row = mysql_fetch_arr ay($db_result)) {
          > $table_key = $row("table_key ");[/color]
          [...]

          Not sure if/what this will fix, but in that last line ($table_key =
          $row("table_key ");), the parantheses in $row("table_key ") should be changed
          to brackets: $row["table_key"]

          - JP


          Comment

          • Phillip T. Murphy

            #6
            Re: Is this Possible - PHP/MySQL


            "kingofkolt " <jessepNOSPAM@c omcast.net> wrote in message
            news:B4ckd.2145 7$5K2.20313@att bi_s03...[color=blue]
            > "Phillip T. Murphy" <pStPmAuMrphy@b ellsouth.net> wrote in message
            > news:bg8kd.753$ z3.279@bignews5 .bellsouth.net. ..[color=green]
            >> Well, after half pulling my hair out messing with this, I am thinking it[/color]
            > is[color=green]
            >> not possible. I did research and found references to "sub-queries" not
            >> possible in MySQL (I am using 4.0.18-32 with PHP 4). But, not sure if I[/color]
            > am[color=green]
            >> breaking that rule or not.
            >>
            >> Psuedo code of what I am trying to do...
            >>
            >> <?php
            >>
            >> $sql = "select * from table1";
            >> $db_result = mysql_query($sq l);
            >>
            >> while ($row = mysql_fetch_arr ay($db_result)) {
            >> $table_key = $row("table_key ");[/color]
            > [...]
            >
            > Not sure if/what this will fix, but in that last line ($table_key =
            > $row("table_key ");), the parantheses in $row("table_key ") should be
            > changed
            > to brackets: $row["table_key"]
            >
            > - JP
            >
            >[/color]

            Thanks for the responses everyone. Thanks to suggestions here and assuring
            me that it is possible to do nested queries, I got it to work.

            Come to find out, it had to do with the way I was checking if the second
            query returned anything or not. When it didn't return anything, I was
            checking one of the array variables to see if it was NULL or Blank. That
            did not work. I backed up and now check what the mysql_query returns (FALSE
            or an Array).

            All is now well. As always, coming to a group like this to get answers fast
            solved my problem.


            Comment

            Working...