Not only does this happen frequently to PhD students, it also happens occasionally to experienced researchers. Many research projects are embarked upon in a somewhat speculative manner, where we do not really know what the answer to a question will look like until we get there. Sometimes we get stuck and cannot get the answer to our research question, sometimes we get the answer but it is so trivial that we realise we were thinking about the scope of the problem incorrectly, and sometimes we get the answer and we then find out that others have already figured out and published the same answer before us (which is something that does not always show up in your initial literature review).
It is less common for experienced researchers to completely abandon projects, because we have pretty good a priori judgement of what will work, and we are also good at salvaging work if it does not turn out as planned. Moreover, many academics tend to put unsuccessful projects "on hold" and come back and think about them periodically to see if they can be changed in some way to give fruitful research. During my own PhD candidature and my subsequent research career as an academic, I have started and done work on several topics which I have subsequently "put on hold" or altered to such a degree that the initial topic was essentially abandoned. I have had other topics where I did a whole lot of research, even wrote up a paper, and then subsequently found that I had merely rediscovered results that were already in the literature (under a name I was not familiar with). I also have probably ten or twenty started ideas sitting around and stalled because I have encountered some roadblock that may or may not be fatal to the future of the project. As you will see from some examples below, I have had some absolute doozies.
Mine have come about because I did initial literature reviews that were flawed, and I didn't realise that my research ideas were things that were already well-developed in the literature (under technical names I was not familiar with). In terms of how you cope, well, you look at the silver lining --- often these aborted projects show that you are "on the right track" in terms of your ability to come up with interesting ideas, even though these do not pan out into publications. If you are developing projects that are good enough for other researchers to "take them" then that means you are on the right track. (In my case, other researchers were nefarious enough to steal my ideas and complete them decades before I was born!) It is depressing when you think you have a great idea and then it turns out not to lead to anything, but you look on the bright side --- it is better than having no good ideas at all.
Some (kind of embarrassing) examples from my own research career
Rediscovering the theory of identifiability: When I was in the first year of my PhD in statistics I came across an interesting problem that I thought would be a wonderful PhD topic and I spent many months solving it and writing up an academic paper for a journal. I was extremely happy with my paper and thought it would be a big deal, since it seemed to me that I had developed an important concept that would be a great addition to statistics. A couple of weeks later I got a desk rejection from the journal, and the editor was kind enough to gently inform me that while my paper looked very interesting, and was well written, my ideas "look a lot like the theory of identifiability" (a term I had not heard of at that time). Using this new term I did another quick literature search and discovered a huge literature; my own paper had essentially rediscovered an important mathematical/statistical concept that was already developed and published in about the 1950s-1960s. I had managed to get through my undergraduate degree without hearing this term, and so it had not shown up in my initial literature search, and my supervisors also did not alert me to it when I showed them what I was working on. So, I have the "distinction" of being one of the discoverers of the theory of identifiability (never published), which I discovered about fifty years after its original publication! It was depressing at the time because I had done a lot of work on it, but now I look back and laugh about it.
Rediscovering the theory of constrained optimisation via penalty functions: This one came a bit after I had finished my PhD, when I was an early career academic. I had done a bunch of work on constrained optimisation (Karesh-Kuhn-Tucker method, etc.) and I had thought of an idea of an alternative way of doing constrained optimisation that I thought was novel. Again, I tried searching for it in the literature, but I used the wrong words so it didn't show up. (I think I was calling the method "optimisation via augmentation" which is not its standard name.) I spent my Christmas holidays working on a paper on my method, and was happy with how it developed. When the paper was almost finished, by accident (when looking at another problem) I found a reference to a paper that led me to another paper that alerted me to the literature on optimisation via penalty functions. I had a look at the papers I had found and boom --- another project destroyed. From memory, this stuff was done in about the 1970s, so this time I was a mere forty years late to the party! In this second case I did not submit my (almost finished) paper, so it sits on my computer as a fun little reminder of my Christmas rediscovering penalty function methods.