S
S
Sorbing2012-06-11 16:00:44
PHP
Sorbing, 2012-06-11 16:00:44

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>

Second:
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

7 answer(s)
H
Hint, 2012-06-11
@Hint

If you choose, then option number 3:

return DB->fetch_column('SELECT code FROM table ...');

1. I don’t understand what could be the plus of the here-line.
2. Why double quotes instead of apostrophes? Are you putting variables in there? If yes, then your option is obviously terrible. Use at least pdo with placeholders.

H
hlx, 2012-06-11
@hlx

$query = '
   SELECT
       code
   FROM
        table
   WHERE
        id = ?';

uses the second option and start on a new line (It will be almost the same as the 1st option).
+ use single quotes
+ use placeholders

I
ixSci, 2012-06-11
@ixSci

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.

A
Anatoly, 2012-06-11
@taliban

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.

V
Vladimir Chernyshev, 2012-06-12
@VolCh

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.

S
Sorbing, 2012-06-11
@Sorbing

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.

P
patashnik, 2012-06-11
@patashnik

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 question

Ask a Question

731 491 924 answers to any question