MyBB Community Forums

Full Version: PHP in SQL, which is better??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
$sql = "SELECT * FROM `table` WHERE `col1` = '{$var1}';"

or:

$sql = "SELECT * FROM `table` WHERE `col1` = '" . $var1 . "';"

Just curious really. Is one any faster than the other??
I prefer the second one but to tell you the truth I haven't really noticed a difference. I think it's more your personal opinion and which way you like to do.

But if I had to do my guess I think the second one will take maybe a tiny little tiny bit longer?
The second is more efficient.
But putting an SQL query in a variable is kind of pointless.

mysql_query("SELECT * FROM `table` WHERE `col1` = '" . $var1 . "';") or die(mysql_error());

Would be your best bet.
Single quotes are faster, but the difference is negligible.
(2009-11-30, 10:44 AM)Yumi Wrote: [ -> ]Single quotes are faster, but the difference is negligible.

I used to be paranoid and always used single quotes for EVERYTHING and never used double quotes except for stuff like \n.

I seriously did stuff like
$myStr = 'this is my string with '.$vars.' here and '.$vars.' there'."\n";

But now I use single quotes when no variable expansion is required. And if there is variable expansion, I prefer to use inline:
$myStr = "this is my string with {$vars} here and {$vars} there\n";
Pretty much for readability purposes...no more littering quote marks everywhere...
(2009-11-29, 09:17 PM)MattRogowski Wrote: [ -> ]Is one any faster than the other??

A smart compiler would take both statements and build the exactly same byte code for both of them, since they do the exact same thing, so both would run at the exact same speed. However, PHP as a scripting language has to compile and execute code in real time, which leaves very very little time to actually analyze and optimize code. Therefore such optimizations are usually just not done.

Unoptimized, the first one is more efficient, but the difference should not be measurable with just a handful of variables.

It becomes interesting however if you have a million variables instead of just one or two.

With "$aaaaaa $aaaaab $aaaaac ... $zzzzzz", the length of the final string can be calculated directly by parsing the string once. Therefore the end result string can be allocated and built directly, which is the most efficient solution.

With $aaaaaa . ' ' . $aaaaab . ' ' . $aaaaac . ' ' ... ' ' . $zzzzzz, you have a million string allocations and join operations, i.e. before reaching the final result you build "$aaaaaa ", then "$aaaaaa $aaaaab", then "$aaaaaa $aaaaab ", then "$aaaaaa $aaaaab $aaaaac", then ...continue a million times until you reach "$aaaaaa $aaaaab $aaaaac ... $zzzzzz".
(2009-11-29, 09:35 PM)xiofire Wrote: [ -> ]The second is more efficient.
But putting an SQL query in a variable is kind of pointless.

mysql_query("SELECT * FROM `table` WHERE `col1` = '" . $var1 . "';") or die(mysql_error());

Would be your best bet.

This is how I do it.
You could always do it the easier way:

"SELECT * FROM table WHERE something='$something_else'"
mysql_query ( "SELECT * FROM `table` WHERE `something` = '" . $something . "';" );

I can't stand when variables aren't properly highlighted. I need to be able to see them without actually having to look closely. Toungue

The difference between the two, however, is hardly noticeable.