I have this code:
if ($firearm != "") {
$sql_result5 = mysql_query("SELECT icon FROM db_firearms WHERE name='$firearm'", $db);
$rs5 = mysql_fetch_array($sql_result5); $firearm_icon=$rs5[icon];
}
if ($item != "") {
$sql_result5 = mysql_query("SELECT icon FROM db_items WHERE name='$item'", $db);
$rs5 = mysql_fetch_array($sql_result5); $item_icon=$rs5[icon];
}
if ($vehicle != "") {
$sql_result5 = mysql_query("SELECT icon FROM db_vehicles WHERE name='$vehicle'", $db);
$rs5 = mysql_fetch_array($sql_result5); $vehicle_icon=$rs5[icon];
}
if ($melee != "") {
$sql_result5 = mysql_query("SELECT icon FROM db_melee WHERE name='$melee'", $db);
$rs5 = mysql_fetch_array($sql_result5); $melee_icon=$rs5[icon];
}
This code is being refreshed every 10 seconds on a part of my page showing the players equipment. Would it be best to have this in one query, to save db resources?
If so, how would I write that? Would it be INNER JOINS? normal JOINS?
You’d use a union, with a bit of fireproofing in case you get no match on a name:
Code example to work around broken DB-design
The
union allstops MySQL trying to eliminate duplicates, speeding up the query.Better solution
You should really put all your icons in one table
Now you can select
But of course this really depends of the details of your application.
My point is just that UNION is a warning sign that you’re doing something wrong or unusual