How to query-and-increase a value (counter) in a thread-safe way? (avoid race conditions)
The update statements works perfectly fine without the select before! Since single statements are safe by definition, even two UPDATE queries performed at the same time only will result in the row incremented twice.

If you actually want to select the value for your PHP script, do something with it and later want to update this exact counter value, you can do the following:

BEGIN;
SELECT `counter` FROM `table` WHERE `id` = 123 FOR UPDATE;
UPDATE `table` SET `counter` = `counter`+1 WHERE `id` = 123;
COMMIT;


This starts a new transaction, then selects the rows you want to update and locks them exclusively. You can then safely update those without worrying about other clients changing their content or even accessing the locked rows. Finally you need to commit your changes.

You should also read something about isolation levels. You likely do not want a value like READ UNCOMMITTED as isolation level. Everything else should be fine for this use-case.