This is known problem with the db api. The problem is that you have two db handles active, one for the db_foreach and one for the db_string call. The db_string handle is not included in the transaction (even thought it appears that it should be), so your select insided the db_foreach loop doesn't return anything, because the transaction which includes the db_dml's hasn't completed yet. In your example, you could replace the db_foreach with a db_string to get the date, and it would work okay.