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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rtblizz
Forum Newbie
Posts: 6
Joined: Mon Mar 12, 2012 8:26 am

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

Post 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, )???
Last edited by rtblizz on Mon Mar 12, 2012 9:59 am, edited 2 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Looks like you forgot the concatenation operator.

Code: Select all

$strquery .= " ".$row["zipcode"]." ";
rtblizz
Forum Newbie
Posts: 6
Joined: Mon Mar 12, 2012 8:26 am

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

Post by rtblizz »

Thanks that helped alot now I have this issue?? any suggestions??
rtblizz
Forum Newbie
Posts: 6
Joined: Mon Mar 12, 2012 8:26 am

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

Post by rtblizz »

Thanks that helped alot now I have this issue?? any suggestions??
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

rtblizz wrote:I need to end the query with a 321) not a 321, )

Code: Select all

$strquery = rtrim($strquery, ', ');
rtblizz
Forum Newbie
Posts: 6
Joined: Mon Mar 12, 2012 8:26 am

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

Post by rtblizz »

bingo
Post Reply