$$\int \frac{dx}{x\sqrt{a^2+x^2}}$$
doing trig substitution with $x = a\tan\theta$ gives me:
$$\frac{1}{a}\ln\left(\tan\frac{\theta}{2}\right)$$
Now I know that $\tan \theta = \dfrac{x}{a}$ which gives a triangle with base $a$ and a height of $x$ but I don't know how to change the variables back in terms of $x$ because of the parameters that tangent receives.
Book answer: $$\frac{1}{a}ln\left(\frac{x}{a + \sqrt{a^2+x^2}}\right)$$
Doing half angle formula I get:
$$\frac{1}{a}ln\left( \frac{\sqrt{a^2+x^2} -a}{x} \right)$$
We know $\tan \theta = \dfrac{x}{a}$ $(1)$. We will use the fact that $\tan{\frac{\theta}{2}}=\frac{1-\cos\theta}{sin\theta}=\frac{1-\sqrt{1-\sin^2{\theta}}}{\sin\theta}$ $(2)$. Now we shall represent $\sin\theta$ as a function of $\tan\theta$, since we know that $\tan \theta = \dfrac{x}{a}$, that way we can represent $\tan\theta$ as a function of $x$. $$\tan \theta=\frac{\sin \theta}{\cos\theta}\implies\tan^2\theta=\frac{\sin^2 \theta}{\cos^2\theta}=\frac{\sin^2 \theta}{1-\sin^2\theta}$$
I'll leave it to you to show that $\sin^2\theta=\frac{\tan^2\theta}{1+\tan^2\theta}\stackrel{(1)}{=}\frac{x^2}{a^2+x%2}$. Now if we plug this into $(2)$ we get the following: $$\tan\frac{\theta}{2}=\frac{1-\sqrt{1-\frac{x^2}{a^2+x^2}}}{\sqrt{\frac{x^2}{a^2+x^2}}}=\frac{1-\sqrt{\frac{a^2}{a^2+x^2}}}{\sqrt{\frac{x^2}{a^2+x^2}}}=\frac{\sqrt{a^2+x^2}-a}{x}=\frac{(\sqrt{a^2+x^2}-a)(\sqrt{a^2+x^2}+a)}{x(\sqrt{a^2+x^2}+a)}=\frac{x}{a+\sqrt{a^2+x^2}}$$ Now you can simply put this into your antiderivative and you'll get the correct solution.
When doing this yourself you need to be careful about the sign of all the functions you're taking root of or squaring.