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

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.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.