Jump to content

How to use results from a MySQL Query while loop in a 2nd Query


beckjoh

Recommended Posts

I first query a table called Points for all rows. This table holds x and y points in longitude and latitude. I then while loop the results to get each row. I then want to run a new query on a second table called segments in which I will use the stored x,y data as a condition or variable in the query. I can get this to run for one point but not for all the points. Can you use data from one table to use for a query in another table? I would try to use JOIN but the tables do not relate, I just need to evaluate each point, the problem is there are hundreds of points. Here is my code.

 

$pointresults = mysql_query("SELECT * FROM points");

$num_points = mysql_num_rows($pointresults);

if($num_points == 0) {

echo "No Points in Table, There is an Error!";

}

else {

// get each row

while($mypointrow = mysql_fetch_array($pointresults))

{

$thelon = $mypointrow['Longitude'];

$thelat = $mypointrow['Latitude'];

 

$result = "SELECT poly_id, Segment_id FROM segments WHERE ( longa > $thelng OR longb > $thelng )

AND ( (lata > $thelat AND latb < $thelat )

OR (latb > $thelat AND lata < $thelat ) )";

 

$result2=mysql_query($result);

$resultnumrows = mysql_num_rows($result2);

 

Any help would be appreciated.

Consistency in variable names helps

 

$thelon = $mypointrow['Longitude'];

$thelat = $mypointrow['Latitude'];

 

$result = "SELECT poly_id, Segment_id FROM segments WHERE ( longa > $thelng OR longb > $thelng )

AND ( (lata > $thelat AND latb < $thelat )

OR (latb > $thelat AND lata < $thelat ) )";

 

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.