Skip to content
Advertisement

How to make SQL query more readable in PHP?

When you have a long fields in SQL query, how do you make it more readable?

For example:

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');
    }

Advertisement

Answer

You can concatenate it like this to make it more readable:

$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";

Note: Be sure while concatinating your query, don’t forget to leave spaces before you start a new line between your double quotes, else you’ll get query invalid error

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement