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.

Link to comment
Share on other sites

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 ) )";

 

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.