I use this:
usage: $emails=create_lookup('emails','email_address');
Will create the array of all email_addresses.
$emails=create_lookup('emails','email_address','email_id');
Will create the lookup with email_id as the key for each element.
function create_lookup ($table,$value_field,$key_field=''){
$q='SELECT '.($key_field?"$key_field as key, ":'')." $value_field as value FROM $table;";
$r=DBresult($q);
$r_numr=DBnumrows($r);
for($i=0;$i<$r_numr;$i++){
$row=DBrow($r,$i);
if($key_field){
$a[$row[0]]=$row[1];
}else{
$a[]=$row[0];
}
}
return $a;
}
Assumptions: Already a valid connection to database.
$table = name of table
$key_field = field to use as key in new array
$value_field = field to use as value in new array
The DB* functions I have created as a wrapper to the database functions (I use postgres).
DBresult - runs the query (aka pg_exec, or whatever)
DBnumrows - gets the number of rows in the result
DBrow - fetch row
hope this helps