I connected to MySQL from PHP using root user. Connection object was $con. The connection was successful.
I wanted to select a particular database so I used mysql_select_db('database', $con), but I got an 'Access denied for user '@'localhost' to database' error.
I can’t understand why I got an access denied error, as I am using root user. I double-checked the grants for root and it has all privileges on .. And I am able to execute queries on this particular database through the command line client using root. I am running everything on my local machine.
You’re not using the root account (which is a BAD idea regardless of what you’re trying to do). The error message clearly states
... for user ''@localhost: there’s no username in there, so you’ve connected with no username at all.