Imagine there's a SQLite database with two tables, "users" and "ranks", one holds user names and their rank as a number (and the userid), the other holds the same rank numbers and the name of the rank. The goal is to output a html table with the name of the rank and the user names (and their userid).
<?php
$db1 = new PDO('sqlite:database');
$db2 = new SQLite3('database');
$result = $db1->query("SELECT * FROM users ORDER BY rank DESC");
echo '<table><tr><td>rank</td><td>username</td><td>id</td></tr>';
$data = "";
foreach($result as $row){
$data .=
'<tr><td>'.$db2->querySingle("SELECT rankname FROM ranks WHERE ranknumber =".$row['rank']).'</td>'.
'<td>'.$row['name'].'</td>'.
'<td>'.$row['id'].'</td></tr>';
}
echo $data;
echo "</table>";
?>
this code works perfectly fine on a small scale, the question is of its performance once the database is highly populated. Essentially it's making a new request to the database for every outputted row of the first query, right? I added the use of $data instead of echoing every single row one by one solely in case it might reduce requests somewhere along the road somehow, but I'm not sure if it'll even make any positive difference. I'm too inexperienced with database handling and php to know how much strain this code would put on the server and/or network compared to simply having 2 queries side by side, will my code essentially perform hundreds if not thousands of database requests and how would that affect performance? Additionally, if it's bad, what can I do about it? Is there any different way to achieve the same results without performance issues?
Even more importantly, what if I also replace the user name with a displayname from an entirely different non-SQLite (MySQL) database altogether? 201 queries to display 100 users, does that mean massively multiplied resource usage?