我更喜欢Heredoc语法,尽管Nowdoc也适用于您的示例:
黑血树:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
诺多克: 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');
}