Answer the question
In order to leave comments, you need to log in
Style of writing native SQL queries?
On a new project, I came across this style of SQL queries. The colleague, got used to write such requests. I'm used to using formatted style with double quotes. I want to hear the opinion of the community about one and the other approach. Thank you.
First:
class SomeClass<br>
{<br>
...<br>
private function getErrorCodes() {<br>
$query = <<<SQL<br>
SELECT<br>
`code`<br>
FROM `table`<br>
...<br>
SQL;<br>
return DB->fetch_column($query);<br>
}<br>
...<br>
}<br>
class SomeClass<br>
{<br>
...<br>
private function getErrorCodes() {<br>
$query = "SELECT<br>
`code`<br>
FROM<br>
`table`<br>
...";<br>
return DB->fetch_column($query);<br>
}<br>
...<br>
}<br>
Answer the question
In order to leave comments, you need to log in
If you choose, then option number 3:
return DB->fetch_column('SELECT code FROM table ...');
$query = '
SELECT
code
FROM
table
WHERE
id = ?';
I'm not a PHP expert, but the first option looks bad. At least with backlighting. And the glued request to the wall, while everything else is done with indents, does not look logical.
What's the difference anyway? And here and there the text, and there and there, the output is a valid sql query that is being executed. I don't see any difference at all.
If PHP could close here/newdoc not from the beginning of the line, it would choose the first option for itself, otherwise the indentation logic is violated, which I love very much, including when mixing languages, without which a rare PHP application can do. I myself use the concatenation option.
The main problem is that 2 developers with different perceptions of the beauty of the code are working on the same project. And you need to find a common denominator. In most of the project (if not the entire project) it is written according to the 2nd option with a request in the HERE line - it hurts my eye, but my colleague is so used to it. I do not pull the blanket over myself, but you need to stipulate a single style. Although, as noted, Hint is better to refactor the project using PDO.
Any sql queries are strings. Therefore, it would be easier to format them the way strings are formatted in your project. One of the popular options is
class SomeClass
{
...
private function getErrorCodes() {
$query = "SELECT"
. "`code` "
. "FROM "
. "`table` "
. "WHERE ...";
return DB->fetch_column($query);
}
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question