I'm having a little trouble selecting a database with php. Here's my problem:

I was working on a Windows machine that was all set up and everything worked well. I had to move over to my Linux box and started httpd and mysql. Everything seems good.

I can access mysql from the command line and everything works wonderfully. But when I try to access it from php via localhost, I can't select a database.

My code that worked on Windows is:
mysql_connect("localhost") or die("Could not connect to MySQL");
mysql_select_db("mydb") or die("Could not connect to database");

My output is "Could not connect to database" which means that I'm connecting fine but not finding or accessing the selected db.

The only thing I can think of is a permissions issue with Linux. I've been Googling this problem for a while with no possitive results, this is my last resort.

I'm not sure the best place to post this question 'cause it could be many problems (php, mysql, linux) but this is the forum I'm most familiar with. If anyone can help me out - or suggest a better place to look for help - I'd appreciate it.

Thanx.

    Could start by changing to:

    mysql_select_db("mydb") or die(mysql_error());

    to see if there's a more verbose error message that can provide hints.

      Good suggestion - I forgot about that.

      The error I get is "Access denied for user: '@' to database 'mydb'"

      So the question becomes - how do I allow '@' to gain access to the db??

        You probably need to do some GRANTing.

        e.g.

        GRANT <permissions> ON mydb TO 'username@localhost'

        ...
        or was it
        'username'@'localhost'

        cant quite remember

          I think you are right - so I read up on GRANT and ended up registering myself:

          grant all on . to 'me'@'%' identified by 'password';

          I check the table and I'm properly listed in it.
          I re-edited my connect function to include myself:

          mysql_connect("localhost","me","password") or die (mysql_error());
          mysql_select_db("mydb") or die (mysql_error());

          But now receive this error:

          "Warning: mysql_connect(): Access denied for user: 'me@localhost' (Using password: YES) in /srv/www/htdocs/dir/file.php on line 10

          Where line 10 is the "mysql_connect(...."

          Somehow I think I'm close - and that is very frustrating.

            6 years later

            Hello there i am also having a cannot connect to database issue but mine is the fact that i get a Unknown database 'comments' i also tried making a database bob and still same issue

              Write a Reply...