I always hated writing big queries in php by concatenating strings. Its very ugly and can get hard to follow with quotes and double quotes. Heredoc to the rescue. If you’re unfamiliar with heredoc, its basically a syntax with php (and some other perl based languages) to type blocks of text without having to escape characters. This can also be useful when writting our big multi join/union/where/group/having/order by query. Here’s how:

select *
from pages
where pagename='$pn'

You have to be very cautious though with spacing. Heredoc does not like spaces on the lines with the opening and closing identifiers (EOD in this case), however everything in between can be spaced to suit your fancy. Give it a shot!