1

I am using ST_Intersects() to know whether two geometries intersect or not. This is my code:

$var_name = pg_query($conn,"SELECT ST_Intersects(p1.column_name, 
p2.column_name)
FROM table_name AS p1, table_name AS p2
WHERE p1.id < p2.id");
if(pg_fetch_assoc($var_name) == 'false')
{
echo "accepted";
}
else
{
echo "rejected";
}

I know that the geometries are not intersecting and when I run the same query in Postgres the result is false as expected.There are only two rows in the table.

But here it is executing the else statement. Where am I wrong?

closed as off-topic by JGH, whyzar, xunilk, aldo_tapia, Dan C Feb 12 '18 at 15:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

I don't know anything about PHP, but looking at some of the documentation (pg_query and pg_fetch_assoc) I would guess something like the following may work.

$var_name = pg_query($conn,"SELECT ST_Intersects(p1.column_name, p2.column_name) intersects -- Aliased with a name
FROM table_name AS p1, table_name AS p2
WHERE p1.id < p2.id");
if(pg_fetch_assoc($var_name)['intersects'] == 'false')
{
echo "accepted";
}
else
{
echo "rejected";
}

You may also want to echo the result directly to see what is being returned.

That being said though, you are doing a triangular join on your two tables. This will result in many rows that you are not taking into account. Currently you are only testing the first result.

The following maybe more appropriate

$results = pg_query($conn,
    "SELECT p1.id AS p1ID, p2.id AS p2ID, CASE WHEN ST_Intersects(p1.column_name, p2.column_name) THEN 'Accepted' ELSE 'Rejected' END AS Res-- Aliased with a name
     FROM table_name AS p1, table_name AS p2
     WHERE p1.id < p2.id");
while ($row = pg_fetch_assoc($results)) {
    echo "$row['p1ID'] and $row['p2ID']: $row['Res']\n"
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.