When num_rrows give such an error
it means the query failed.
Most often because a syntax error in query.
I will have guess at what is your correct query.
And I will add [man]mysql_error[/man]
so that MySQL will give a hint where you have the syntax issue.
$query = "SELECT $retfield FROM $table WHERE $givefield='$val'";
$res = mysql_query($query);
// check if there was an error executing the query
if(!$res){
echo 'Your QUERY: ' . $query . '<br />MySQL error: ' . mysql_error();
}
if(mysql_num_rows($res) > 0){
}