我更喜欢 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 的行为类似于单引号。
诺德克:
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');
}