Is this Possible - PHP/MySQL

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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...


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

while ($row = mysql_fetch_array($db_result)) {
    $table_key = $row("table_key");

    $sql2 = "select * from table2 where tabel_key='$table_key'";
    $db_result2 = mysql_query($sql2);
    $row2 = mysql_fetch_array($db_result2);

    if record exists in table2
        do something with table 2;
        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,


Re: Is this Possible - PHP/MySQL

from Phillip T. Murphy contained the following:

Quoted text here. Click to load it

Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs /

Re: Is this Possible - PHP/MySQL

 .oO(Phillip T. Murphy)

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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.


Re: Is this Possible - PHP/MySQL

Quoted text here. Click to load it

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

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.

Re: Is this Possible - PHP/MySQL

Quoted text here. Click to load it

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

Re: Is this Possible - PHP/MySQL

Quoted text here. Click to load it

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.

Site Timeline