I have code to generate four cars from a DB with their photos:
$l=$database->query("SELECT car,brand,exactprice FROM $table ORDER BY rand() LIMIT 4");
$buffer="";
foreach($l as $l){
$buffer.="<li><h3>{$l['car']}</h3><p>Price {$l['exactprice']}</p>";
$k=$database->query("SELECT logo FROM logo WHERE brand='{$l['brand']}'");
$buffer.="<img src='{$base_addr}/{$k[0]['logo']}'><br>";
}
That’s my current code. Can I rewrite my MySQL command so I would get logo from the logo with the first select I did?
Currently it takes 4.32 seconds to load this page.
You didn’t supply what
$tablewas, but either way you could use aJOINon the brand column (assuming that this column is the appropriate foreign key).As far as the performance, you could look into creating indexes on the columns you use frequently and benchmarking by adding
EXPLAINin front of the query above.Some references to check out: