MySQL Database Php

function simplequery($table, $field, $needle, $haystack) {
            $result = mysql_query("SELECT $field FROM $table WHERE
                     $haystack = $needle LIMIT 1;");
            if ($result) {
                    if (mysql_num_rows($result)) {
                            $row = mysql_fetch_assoc($result);
                            return $row[$field];
                    }
            } else {
                    print "Error in query";
            }
    }
    $firstname = simplequery("usertable", "firstname", "ID", $UserID);