PHP Tip, writing a MySQL query

When writing a MySQL query, it is not necessary to use escaped double quotes (“) when representing literal strings within the query. Single quotes (apostrophes) will work fine. For example, the following two lines of code will perform the same query:

$result = mysql_query(“SELECT foo FROM bar WHERE baz=”test””, $db);
$result = mysql_query(“SELECT foo FROM bar WHERE baz=’test'”, $db);

As will this code perform the same query:

$var = ‘test’;

$result = mysql_query(“SELECT foo FROM bar WHERE baz=’$var'”, $db);

There is not necessarily a performance gain between one method or the other, but using single quotes instead of escaped double quotes greatly improves the readability of your code.

Comments are closed.