How can I stop myself from having panic attacks?

Panic. Breathing exercises work best. Also prescribed medications.
Panic Attack. If you can find a quiet place - do so. Breath in slowly ; deeply through your nostrils. Fill your lungs with air (but not to the point of discomfort). Hold for a count of five then slowly begin to exhale through an open mouth. Repeat at least 12 times, longer if you need to. If you haven't been assessed for your panic/ anxiety - that would be prudent. Anxiety is no fun but it is treatable.