Well for me, it would depend how much data you are handling and what you are doing with it, but from the little info give, something like this:
Code: Select all
function DropDown() {
static $dbCache = NULL;
if (is_null($dbCache)) {
$dbCache = array();
$sql = "SELECT * from student";
$db->Select($sql, &$result);
while ($nrow = mysql_fetch_array($result)) {
$dbCache[] = $nrow;
}
}
foreach($dbCache as $nrow) {
if (($nrow['id'] == 1) || ($nrow['id'] == 20) {
// do something
}
}
}
I'm used to working with a lot of data and complex joins, so my tendency is that any data that will get called more than once, store it. The first part does that, it creates a static variable, whose value will be held across different calls of the function. If that is null (the first call to the function) then it loads up the data from the database (each additional call it will already have the data, so no need to reload from DB). Then it loops through the data doing what you want.
-Greg