How do you convert a parent-child (adjacency) table to a nested set using PHP and MySQL?

I’ve spent the last few hours trying to find the solution to this question online. I’ve found plenty of examples on how to convert from nested set to adjacency… but few that go the other way around. The examples I have found either don’t work or use MySQL procedures. Unfortunately, I can’t use procedures for this project. I need a pure PHP solution.

How to repair a corrupted MPTT tree (nested set) in the database using SQL?

I have an MPTT tree of over 100,000 records stored in MySQL using lft, rght and parent_id columns. Now the left/right values became corrupted, while the parent ids are still intact. It would require tons of queries to repair it in the application layer. Is there a good way to put the burden on the database and have it recalculate the left/right values using only SQL?