php Mysql - PDO 错误 - 无效的目录名称:1046 未选择数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28754835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:02:57  来源:igfitidea点击:

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

phpmysqlpdocpanel

提问by Benss

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:

我对 PDO 有问题,我完全看不到他来自哪里。我不能质疑我的 MySQL 数据库。只是为了测试,我使用了以下代码(之前为连接配置了相当糟糕的参数:

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:

连接与 MySQL 正确建立,但查询后我收到此错误:

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.

致命错误:未捕获的异常 'PDOException' 带有消息 'SQLSTATE[3D000]:无效的目录名称:1046 未选择数据库 /home/outout/public_html/file.php:16 堆栈跟踪:#0 /home/outout/public_html/ file.php(16): PDO->query('select * from t...') #1 {main} 在第 16 行的 /home/outout/public_html/file.php 中抛出。

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?

该代码可在本地计算机上运行,​​但是一旦我将其放到网上 (cPanel),它就会向我显示此错误。我必须在 .htaccess 中配置 PDO 吗?

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

我完全不明白问题出在哪里。有人会有想法吗?

回答by Codehonor

Help Mysql resolve the handle by.

帮助Mysql解析句柄。

Instead of:

代替:

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

Try:

尝试:

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

回答by Faiz Ahmed

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

我遇到了同样的问题,但没有用 'mysql' (mysql:host) 提及 'host' 字符串。我通过以下代码解决了这个问题。

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;
    }
}

回答by Aditya Wankhede

Instead of:

代替:

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

just do:

做就是了:

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

You will be good to go.

你会很高兴的。

回答by Prabhat Kumar

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);

我遇到了同样的问题。问题的解决方法是删除主机名和 dbname 变量之间的空格,如下所示:来自: $conn = new PDO("mysql:host = $servername;dbname = $dbname", $username, $password);

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

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

回答by Gabryel Valentin N?stase

$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:

这里是错误的。你不能有host=serverName,相反你需要有类似host=$serverName 的东西,然后是$serverName = "servername.domainin"; 与 dbname=Mydatabase 相同,您需要创建这些变量,$Mydatabase="mydatabase" 应该如下所示:

$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

这会起作用。更多详情: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 !

我有同样的错误,这是因为我没有在字符串中使用 dbname=$dbname,而是使用了 $dbname=$dbname,只是一个错字。您需要正确编写字符串!

回答by Murph

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.

让我失望的是我从 MySQL 工作台中的树中复制了数据库名称并将其粘贴到 settings.php 文件中。原来字符串开头和结尾的撇号是假撇号!我删除了它们并添加了真正的撇号,然后一切都启动了。我花了几个小时才弄清楚这一点。希望这对其他人有帮助。

回答by mzhtuhin

Use

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

instead of

代替

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