How I solved the "access denied to `user`@`localhost`" MySQL error in PHP MySQL
Tags: php, mysql, cpanel, webdev
Published: 2022-08-20 15:09:09

## Firstly How to handle bugs

When you encounter a very difficult bug and it is driving you crazy, I recommend you do these things they worked for me;
1. Take a break, you break can span several days
2. Go and Sleep
3. Think about it
4. If you are a theist, Pray to God (not Joking, because some bugs are actually village people 🤣)
After doing all this things one day you'll get an answer, I just had a serious experience with a bug and I'm sharing it in full detail below;

**RECENTLY**, I was working with a website I'm building hosted live, so I worked on the MySQL database part and decided to update the one hosted live, well I tested it after updating it and it didn't work, in the error log file on the cpanel host, I discovered that access is not been granted to the database user, which was strange to me, ALL access was granted to the database user, several contacts were made to namecheap support thinking it was from their side, but it still didn't work.
I was frustrated, I left the work for sometime while I thought about the problem and researched about it online and on forums, after some weeks and this morning by after 7 am, something told me to prefix the database name to the table name whenever I'm making a query (as in `database_name`.`table_name`), I tried that and it worked, Omo, I was like..., revitalized and happy.


## So My Error All This While
Whenever I'm working offline with the WAMP server, you can make MySQL queries without prefixing the database name to the table name and it will work, and it has worked for me even on a live host but now I realize things are quite different for namecheap host and you must prefix the database name to the table name before any query could be made successfully.

## My Observations
So the website uses a shared hosting whose MySQL is connected to one MySQL root user, this made to think that if you are actually making a query like this "SELECT * FROM `users`", you may get errors in a live server using shared hosting because in a shared hosting many other people are using it and there can be many MySQL tables called users that belong to different other website other than yours and making a query without prefixing the database name (which will be unique to your cpanel account) with the table name will lead to errors on a live host.

Different Host works differently, I've had an experience with 000webhost and I discovered it doesn't support `<<<_END` in PHP
I have learnt a lot from this and I am happy to share, I'll definitely publish this somewhere.

## About Me and My Services
I am a full stack web developer with years of experience, I design the Frontend of websites with HTML, CSS and JavaScript and applications and also the backend of web applications with PHP, I will build you your website with amazing responsiveness and great quality.

See my portfolio site - https://johannes.w3spaces.com
Also see this website which I built for a floricultural company - https://floraenterprises.w3spaces.com
and see this website which I built for a fashion house - https://akoredeclothing.netlify.app
another of my awesome projects (A mini virtual assistant built with HTML, CSS and JavaScript) - https://bububot.netlify.app
Also a fact site built with HTML, CSS, JavaScript with its backend powered by PHP and databases with MySQL - https://factkingdom.000webhostapp.com

Trust me with handling your projects, and you shall not regret even a bit, I am looking forward to work with you.

Thank you for reading, I hope you've learnt a lot