Below is just a mockup of 2 codes in php/mysql.
The first one returns a mysql result and run a while loop to iterate the results, the second one does almost the same thing but instead puts the result into a query and then puts it to screen. Now for a more OO approach the second method would be better I think (not sure, i'm learning) however where I am confused is, wouldn't using the second method below use twice as much memory? Isn't it basicly creating 2 arrays instead of 1?
Bir diziye koyarak çok daha esnek gibi görünüyor, bana ipuçları / tavsiye verin. Bu basit bir şey, ya bir sayfadaki dizi aynı sayfada yapılmış olabilir 5.000 kullanıcı adlarını ve dizide userID numarasını ve birden fazla sorgu içerebilir sitemde bir sayfada gerçek dünyadan bir örnek 1 kez koştu olmaz aklınızda bulundurun farklı veri ile.
<?PHP
// first method without using array
$url = "http://www.google.com/";
$sql = "SELECT user_id, nickname FROM `".TABLE_USERS."`
WHERE referrer LIKE '".$db->escape($url)."%'
ORDER BY nickname DESC
LIMIT 0,10";
$rows = $db->query($sql);
while ($record = $db->fetch_array($rows)) {
echo "<tr><td>$record[user_id]</td>
<td>$record[nickname]</td></tr>";
}
//////////////////////////////////////////////////////////////////////
//second method using array
$url = "http://www.google.com/";
$sql = "SELECT user_id, nickname FROM `".TABLE_USERS."`
WHERE referer LIKE '".$db->escape($url)."%'
ORDER BY nickname DESC
LIMIT 0,10";
// feed it the sql directly. store all returned rows in an array
$rows = $db->fetch_all_array($sql);
// print out array later on when we need the info on the page
foreach($rows as $record){
echo "<tr><td>$record[user_id]</td>
<td>$record[nickname]</td></tr>";
}
?>