Mysql - PDO Error - Invalid catalog name: 1046 No database selected

*爱你&永不变心* 提交于 2019-12-23 15:08:50

问题


I have a problem with PDO, and I see absolutely no where he come. I can not question my MySQL database. Just to test I used the following code (having quite sour previously configured the parameters for the connection:

try {
    $dbh= new PDO('mysql:host=serverName;dbname=Mydatabase','user','password');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e) {
    die('Erreur : ' . $e->getMessage());
}

var_dump($dbh);    // gives : object(PDO)#1 (0) { }

$res=$dbh->query('SELECT * FROM table');

The connection is made correctly with MySQL but after query I get this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in /home/outout/public_html/file.php:16 Stack trace: #0 /home/outout/public_html/file.php(16): PDO->query('select * from t...') #1 {main} thrown in /home/outout/public_html/file.php on line 16.

The code works on a local machine, but as soon as I put it online (cPanel) it shows me this error. Do I have to configure PDO in .htaccess?

I absolutely do not understand where the problem come. Someone would have an idea?


回答1:


Help Mysql resolve the handle by.

Instead of:

$res=$dbh->query('SELECT * FROM table');

Try:

$res=$dbh->query('SELECT * FROM Mydatabase.table');



回答2:


I faced the same problem while not mentioning the 'host' string with 'mysql' (mysql:host). I solved the problem by following code.

class Database{
    protected static $pdo;
    public function __construct(){}

    public function connect(){
        $dsn="mysql:host=localhost;dbname=dbname";
        try{
        self::$pdo = new PDO($dsn,"root","");
        self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        echo 'Connected';
        return self::$pdo;
        }catch(PDOException $ex){
            echo $ex->getMessage();
        }
        return false;
    }
    public function getConnection(){
        return self::$pdo;
    }
}



回答3:


Instead of:

$res=$dbh->query('SELECT * FROM table');

just do:

  $res=$dbh->query('SELECT * FROM dbname.tablename');

You will be good to go.




回答4:


$dbh= new PDO('mysql:host=serverName;dbname=Mydatabase','user','password');

This here is wrong. You can't have host=serverName, instead you need to have something like host=$serverName, and then the $serverName = "servername.domanin"; the same with dbname=Mydatabase, you need to make those variables, $Mydatabase="mydatabase" it should look like this:

$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";

    try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "Connection ok!";
    } catch (PDOException $e) {
    echo "Err: " . $e->getMessage();
    }

    $conn = null;

this will work. More details on: http://www.w3schools.com/php/php_mysql_connect.asp

I had the same error, and it was because instead of using dbname=$dbname in string i used $dbname=$dbname, just a typo. You need to write the string correctly !




回答5:


I faced same problem. Resolution of problem was removed the space between hostname and dbname variable as follows: From: $conn = new PDO("mysql:host = $servername;dbname = $dbname", $username, $password);

To: $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);




回答6:


What tripped me up was that I copied the database name from the tree in MySQL workbench and pasted it into the settings.php file. Turns out that the apostrophes at the beginning and end of the string are fake apostrophes! I deleted them and put in real apostrophes and everything fired right up. Took me several hours to figure this out. Hope this helps someone else.




回答7:


Use

$database -> query(' SELECT * FROM "database_name"."table_name" ');

instead of

$database -> query(' SELECT * FROM "table_name" ');


来源:https://stackoverflow.com/questions/28754835/mysql-pdo-error-invalid-catalog-name-1046-no-database-selected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!