Question

PDO Unknown database

Getting an error stating that the database is unknown for my user even though it exists and is spelled correctly. I have granted them all permissions on the db, I can access it through the mysql command line in my server and I can access it through mysql workbench but when trying to access it with pdo in my api.

I checked the User, Host from mysql.user and it shows that the user has % and localhost. I commented out bind-address in mysqld.cnf and created my user like so.

CREATE USER 'user'@'localhost' IDENTIFIED BY 'myPassword';
CREATE USER 'user'@'%' IDENTIFIED BY 'myPassword';
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

My PDO Connection

<?php
class Database{
    private $host = "127.0.0.1";
    private $db_name = "casinoCheckList";
    private $username = "user";
    private $password = "myPassword";
    public $conn;

    public function getConnection(){

        $this->conn = null;
        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->exec("set names utf8");
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
?>

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

@paulie5457

I would speculate that the issue you had was based on the transport method being used.

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' WITH GRANT OPTION 
GRANT ALL PRIVILEGES ON casinoChecklist.* TO 'user'@'localhost'

Notice your use of ‘localhost’. What we tend to experience is the client connection using the unix socket and avoiding the TCP/IP routing. Your PHP script uses the loopback address and it might have manifested in the error you experienced.

If you add a user where the host is wild carded or explicitly using the lo interface (127.0.0.1) you may not have experienced this issue.

Something to watch if you’re troubleshooting connection challenges in the future.

BR

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello,

What I could suggest is checking if the casinoCheckList database actually exists:

SHOW DATABASES;

If the database is not there you could create it with:

CREATE DATABASE casinoCheckList;

If this is not the case I could suggest sharing the exact error that you are getting here so I could try to advise you further.

Regards, Bobby