I ran across this gem recently on StackOverflow:
$queryxyzzy12=("SELECT * FROM visitorcookiesbrowsing ORDER by id ASC");
$resultxyzzy23=mysql_query($queryxyzzy12) or die(mysql_error());
while($ckxf = mysql_fetch_assoc($resultxyzzy23)){
$querycrtx=("SELECT * FROM cart WHERE userkey='$ckxf[usercookie]' ORDER by datebegan DESC");
$resultcrtx=mysql_query($querycrtx) or die(mysql_error());
------ snip ------
}
Besides the interesting variable names used, it’s also doing a query inside a loop, which is very inefficient. This would be better written as a single JOIN query:
SELECT
v.*,
c.*
FROM
visitorcookiesbrowsing v
LEFT JOIN cart c ON c.userkey=v.usercookie
ORDER BY
v.id ASC,
c.datebegan DESC
The details of JOIN vs. LEFT JOIN depend on the actual intent of the code, which wasn’t apparent from the snippet.
If at …
[Read more]