$result = mysql_query(" SELECT * FROM '$table' WHERE '$id' = '$idval' ", $db)
That's problematic since single quotes should not be used to delimit identifiers. Double quotes should be used, or in this case for MySQL, backticks.
I suggest two possible approaches:
// #1
function recordExists($id, $idval, $table, $db) {
$result = mysql_query("SELECT `$id` FROM `$table` WHERE `$id`='$idval' LIMIT 1", $db)
OR die(mysql_error());
return mysql_num_rows($result) ? true : false;
}
// #2
function recordExists($id, $idval, $table, $db) {
$result = mysql_query("SELECT COUNT(`$id`) FROM `$table` WHERE `$id`='$idval' LIMIT 1", $db)
OR die(mysql_error());
return mysql_result($result, 0) ? true : false;
}