MySQL Database Php

/*
Description:
This function takes several arguments and generates
the ... HTML code fragment. A mysql_connect() is presumed
to exist already.
Arguments passed:
$dbname (required) Database name
$tblname (required) Table name
$tblkeyvarname (required) Name of table's key field
$tblkeyvaluesname (required) Name of column that describes key field (usually the
displayable value in the displays
this value by default
Return Value(s) $success: returns 1 if query was successful, 0 if query
failed
Usage: To use this, construct the following code in your .PHP file --
- - - - - - - - - - - - -


...

...

PopulateSelectOptions ( $dbname,
$tblname,
$tblkeyvarname,
$tblvaluesname,
$defval
);
?>

...


- - - - - - - - - - - - -
-----------
START FN:
-----------
*/
function PopulateSelectOptions ( $dbname, $tblname, $tblkeyvarname, $tblvaluesname,
$defval ) {
// Check passed arguments
if ( empty($dbname) ) {
echo "\n";
return 0;
}
if (empty($tblkeyvarname) ) {
echo "\n";
return 0;
}
$success=0;
// Set up table query
$local_qry = "select * from " . $tblname;
$localresult=mysql_db_query( $dbname, $local_qry );
echo "";
// Construct HTML code
if ($localresult) {
while( $row=mysql_fetch_array($localresult) ) {
echo " if ( isset($defval) ) {
if ( $row["$tblkeyvarname"]==$defval ) {
echo " selected ";
}
}
if (is_string($tblkeyvarname)) {
echo " value='".$row["$tblkeyvarname"]."'>";
} else {
echo " value=".$row["$tblkeyvarname"].">";
}
echo $row["$tblvaluesname"];
echo "\n";
}
$success=1;
}
// Check if query was successful
if (!$success) {
if (!$localresult) {
echo "\n";
}
}
return $success;
}
?>