Is there a way to query for a set time from a PHP script?

I want to write a PHP script that takes an id and then queries the MySQL database to see if there is a match. In the case where another user may have not yet uploaded their match, so I am aiming to query until I find a match or until 5 seconds have passed, which I will then return 0.

In pseudocode this is what I was thinking but it doesn't seem like a good method since I've read looping queries isn't good practice.

```
$id_in = 123;
time_c = time();
time_stop = time + 5; //seconds

while(time_c < time_stop){
     time_c = time()
     $result = mysql_query('SELECT * WHERE id=$id_in');
}

```