Jump to content

query ->array array->variable query ->variable as criteria


simon551

Recommended Posts

Hi,

Can you use an array as a criteria in a new query?

just wondering if it is possible to do something like this:

 

$queryA="SELECT * FROM table1 WHERE x=1";

$resultA=mysql_qry($queryA);

 

while ($array=mysql_fetch_array($resultA))

    {

          $queryB="SELECT * FROM table2 WHERE y IN($array)";

    }

 

Thanks in advance,

-s

Link to comment
Share on other sites

Thanks! That works.

 

What if I wanted to use this in a page without writing the whole query in to use it inline.

 

I would normally keep my queries in a separate page. This works:

 

$queryA="SELECT * FROM table1 WHERE x=1";
$resultA=mysql_qry($queryA);

while ($array=mysql_fetch_array($resultA))
     {
        $queryB="SELECT * FROM table2 WHERE y IN($array[x])";

do
{
	echo $row['y_1'].'<br />;
}
	while ($row=mysql_fetch_assoc($queryB));
     }		


     }

 

But feels like it could be a bit much although I'm too new to really know.

This doesn't work but I'm not sure why:

 

$queryA="SELECT * FROM table1 WHERE x=1";
$resultA=mysql_qry($queryA);

while ($array=mysql_fetch_array($resultA))
     {
        $queryB="SELECT * FROM table2 WHERE y IN($array[x])";
     }	
do
{
	echo $row['y_1'].'<br />;
}
	while ($row=mysql_fetch_assoc($queryB));

I realize your getting away from the while array logic, but it would be much cleaner to be able to write it like this in the page ( just the echo part without the query.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.