如何在PHP中使SQL查询更具可读性?

时间:2012-10-08 14:00:38

标签: php mysql readability code-readability

如果SQL查询中包含长字段,那么如何使其更具可读性?

例如:

public function findSomethingByFieldNameId($Id) {
        $sql = "SELECT field1, field2, field3 as Field3_Something, field4, field5, field6, field7, field8, field9
                      FROM table
               JOIN table2 AS TNS ON TNS.id = table.id
                      WHERE something = 1";
 return $this->db->fetchData($sql, null, 'all');
    }

7 个答案:

答案 0 :(得分:16)

我更喜欢Heredoc语法,虽然Nowdoc也适用于你的例子:

定界符:

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Nowdoc: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

两者的优势在于您可以直接将SQL复制并粘贴到此块中,而无需转义或格式化它。如果你需要包括解析,比如你用双引号字符串中的变量,你就会使用Heredoc。 Nowdoc的行为类似于单引号。

<强> Nowdoc:

public function findSomethingByFieldNameId($Id) {
    $sql = <<<'SQL'
    SELECT field1, field2, field3 as Field3_Something, field4, field5, field6, field7, field8, field9
    FROM table
    JOIN table2 AS TNS ON TNS.id = table.id
    WHERE something = 1
SQL;

    return $this->db->fetchData($sql, null, 'all');
}

<强>定界符:

public function findSomethingByFieldNameId($Id) {
    $sql = <<<SQL
    SELECT field1, field2, field3 as Field3_Something, field4, field5, field6, field7, field8, field9
    FROM table
    JOIN table2 AS TNS ON TNS.id = table.id
    WHERE something = '$Id'
SQL;

    $sql = mysql_real_escape_string($sql);

    return $this->db->fetchData($sql, null, 'all');
}

答案 1 :(得分:9)

您可以像这样连接它以使其更具可读性:

$sql = "SELECT field1, field2, field3 as Field3_Something,";
$sql.= " field4, field5, field6, field7, field8, field9";
$sql.= " FROM table JOIN table2 AS TNS ON TNS.id = table.id";
$sql.= " WHERE something = 1";

注意:请确保在查询查询时,不要忘记在双引号之间开始换行之前留空格,否则会导致查询无效错误

答案 2 :(得分:4)

我使用免费工具@ http://www.sqlinform.com

<?php
public function findSomethingByFieldNameId($Id) {
    $sql = "SELECT field1                    ,
                   field2                    ,
                   field3 AS Field3_Something,
                   field4                    ,
                   field5                    ,
                   field6                    ,
                   field7                    ,
                   field8                    ,
                   field9
            FROM   TABLE
                   JOIN table2 AS TNS
                   ON TNS.id = table.id
            WHERE  something = 1";

    return $this->db->fetchData($sql, null, 'all');
}
?>

答案 3 :(得分:0)

 $sql = "SELECT field1,
                field2,
                field3 as Field3_Something,
                field4,....
         FROM table
         JOIN table2 AS TNS ON TNS.id = table.id
         WHERE something = 1";

答案 4 :(得分:0)

<?php
   public function findSomethingByFieldNameId($Id) {
        $sql = "SELECT 
                    field1, 
                    field2, 
                    field3 as Field3_Something, 
                    field4, 
                    field5, 
                    field6, 
                    field7, 
                    field8, 
                    field9
                FROM 
                    table
                JOIN table2 AS TNS 
                    ON TNS.id = table.id
                WHERE 
                    something = 1";
        return $this->db->fetchData($sql, null, 'all');
}
?>

答案 5 :(得分:0)

这只是另一种方式。

请注意,数组连接比字符串连接更快。

$sql = join(" \n", Array(
    'SELECT ',
    '    [...fields...]',
    '    [...more fields...]',
    'FROM table',
    'JOIN table2 AS TNS ON TNS.id = table.id',
    'WHERE something = 1',
));

答案 6 :(得分:0)

我个人认为sprintf是最好的方法,因为它的结构与某些SQL Server接受的prepared statement非常相似。

$sql = sprintf(
    'SELECT
        field1 as something,
        field2,
        field3 as Field3_Something,
        field4,
        field5,
        field6,
        field7,
        field8,
        field9
    FROM table
    JOIN table2 AS TNS 
        ON TNS.id = table.id
    WHERE something = %s',
    1
);

我尝试使用Heredoc方法,但是如果您使用的是对象而不只是单个变量,它会使事情复杂化。