i think i know the answer ( method 2 ), but which of these methods would be less taxing on the server:
method 1
PHP Code:
<?
$result=mysql_query("select id from contacts where field='$field'");
while($row=mysql_fetch_row($result))
{
$id_array[]=$row[0];
}
foreach($id_array as $each)
{
$result=mysql_query("select * from contacts where id='$each'");
while(row=mysql_fetch_row($result)
{
print $row['f_name'] . " " . $row['l_name'] . "<br>\n\n" .
$row['addr_1'] . "<br>\n" . $row['addr_2'] . "<br>\n" .
$row['city'] . " " . $row['state'] . " " . $row['zip'] . "<br><br>\n\n";
}
}
?>
method2
PHP Code:
<?
$result=mysql_query("select id from contacts where field='$field'");
$i=0;
while($row=mysql_fetch_row($result))
{
$id[$i]=$row['id'];
$f_name[$i]=$row['f_name'];
$l_name[$i]=$row['l_name'];
$addr_1[$i]=$row['addr_1'];
$addr_2[$i]=$row['addr_2'];
$city[$i]=$row['city'];
$state[$i]=$row['state'];
$zip[$i]=$row['zip'];
}
foreach($id as $each)
{
print $f_name . " " . $l_name . "<br>\n\n" . $addr_1 . "<br>\n" .
$addr_2 . "<br>\n" . $city . " " . $state . " " . $zip . "<br><br>\n\n";
}
?>
i know i could print the results of method 2 in the original loop, however in the project i'm working on , it is in a class function and its purpose is to set object variables.
basically i'm wondering of it is better to put everything in an array even if the data within the array is going to be a lot.. or just run a bunch of sql queries.
which method is optimal?