Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy. This forum is not for asking programming related questions.
I am recently facing problems indenting queries for processing by PHP. I am looking for suggestions/best practices on how to format/indent queries in PHP?
$query = "
select
field_1,
field_2
from
table_1 as a
left join
table_2 as b on a.field_3 = b.field_1
where
a.field2 > 30
group by
a.field_2
order by
a.field_1";
Just the way I do it... I find it easier to work through on some of the loooooooooong queries I write.
$sql = "SELECT field_1, field_2
FROM table_1 AS a
LEFT JOIN table_2 AS b
ON a.field_3 = b.field_1
WHERE a.field2 > 30
GROUP BY a.field_2
ORDER BY a.field_1";
$query = "SELECT foo FROM bar";
$query2 =<<<SQL
SELECT foo
FROM bar
INNER JOIN whiiii USING (blah)
WHERE x > Y
AND location in (
SELECT location
FROM fooloations
WHERE xx = foo.id
)
UNION
SELECT foo
FROM bar2
SQL;
I don't like having to constantly hit shift or capslock while I'm typing lol... I practically always do them all in lowercase. Granted, really short queries just get bundled onto one single line.
My fingers are long enough to reach at the shift keys What i don't like is that i have to move my hands from the keyboard to the mouse It slows me down. That is why i like CLI programs
function somefunction(){
if (somecondition){
$query = "
select
field_1,
field_2
from
table_1 as a
left join
table_2 as b on a.field_3 = b.field_1
where
a.field2 > 30
group by
a.field_2
order by
a.field_1";
$result = mysql_query($query);
}
}
In a situation like the above, heredocs won't work I suppose. It's always a pain to indent in this kind of situation in editors other than Dreamweaver where you have to use so many tabs to get the query in desired format.
The "official" way to do it is CAPS for all keywords and to backtick (`) all your table names and column headings and to line break before every key word.