Page 1 of 1

How can I use results from one query in a second query?

Posted: Mon Mar 12, 2012 8:51 am
by rtblizz
I am retrieving results from a query and trying to insert them into a second query..

Code: Select all

 $sqlstring3 = "SELECT ///// ";

    $result2 = mysql_query($sqlstring3);

while ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    {
		$strquery .= " ".$row["numbers"].", ";
    }
	
	mysql_free_result($result2);
that code does this

213, 321, 123, 221, 321,

which is great but how do insert that into the next query? I am currently doing this.

Code: Select all


$query = "select * from ??? where number IN (".$strquery.") order by Value";

the problem is it gives me this result.

Code: Select all


$query = "select * from ??? where number IN (213, 321, 123, 221, 321, ) order by Value";

which does not work.. I need to end the query with a 321) not a 321, )

any help on how to alter this

Code: Select all


while ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    {
		$strquery .= " ".$row["numbers"].", ";
    }



to get the last value to do 321) not a 321, )???

Re: How can I use results from one query in a second query?

Posted: Mon Mar 12, 2012 8:57 am
by Celauran
Looks like you forgot the concatenation operator.

Code: Select all

$strquery .= " ".$row["zipcode"]." ";

Re: How can I use results from one query in a second query?

Posted: Mon Mar 12, 2012 9:35 am
by rtblizz
Thanks that helped alot now I have this issue?? any suggestions??

Re: How can I use results from one query in a second query?

Posted: Mon Mar 12, 2012 10:12 am
by rtblizz
Thanks that helped alot now I have this issue?? any suggestions??

Re: How can I use results from one query in a second query?

Posted: Mon Mar 12, 2012 10:14 am
by Celauran
rtblizz wrote:I need to end the query with a 321) not a 321, )

Code: Select all

$strquery = rtrim($strquery, ', ');

Re: How can I use results from one query in a second query?

Posted: Mon Mar 12, 2012 11:11 am
by rtblizz
bingo