You don't state why it's not working, but my guess is that in //insert into events table code below your are using the $newEventId in the INSERT, and it works fine if the initial $newEventId causes (mysql_num_rows(mysql_query("SELECT id FROM events WHERE EventID=$gen" )) == 0) to return true, but not if it returns true on the recursive function.
Try returning the value on the break and using that instead of $newEventId
function EventId($gen)
{
if (mysql_num_rows(mysql_query("SELECT id FROM events WHERE EventID=$gen" )) == 0) {
return $gen; // return the number that will be set as $EventId and used in the insert
break;
}
// recall function
else
{
$newEventId = rand(1000, 10000);
EventId($newEventId);
}
}
$newEventId = rand(1000, 10000);
$EventId = EventId($newEventId); // set the returned $gen to $EventId to be used in the Insert query
//insert into events table
mysql_query("INSERT INTO events ... EventID=$EventId..");
EventId()do/mean?breakstatement after theif? It makes no sense. And it seems like you're missing an opening{.