I've never actually fainted, but here's something I was taught at martial arts class:
When you're out of breath, *do not* compress your body by bending at the waist or hunching. That just limits the amount of air your lungs can get. Instead, stand up straight and walk around slowly. It's a good idea to put your hands behind your head, inmate-style, to help expand your rib cage and keep your body upright.
The walking helps keep your legs from cramping.
I've used the above exercise in exactly the types of situations you're describing: lungs on fire, hyperventilating, feeling ill.
What a scary experience! Don't let it discourage you from hard rides, but definitely don't let your ego get in the way of knowing when to quit. (That's something I do all the time =/ )