9

我正在尝试在我的主机上设置一个新站点(如果重要的话,主机路由)但是当我尝试使用 PDO 时我不断收到此错误(我正在尝试第一个 PDO 站点):

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in /home/kennyi81/public_html/gamersite/login.php:36 Stack trace: #0 /home/kennyi81/public_html/gamersite/login.php(36): PDOStatement->execute() #1 {main} thrown in /home/kennyi81/public_html/gamersite/login.php on line 36

当我使用这些设置时:

$dbh = new PDO("mysql:91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

....

$stmt = $dbh->prepare('SELECT * FROM USERS WHERE ID = :id LIMIT 1');

数据库的布局方式:

在此处输入图像描述

我可以在我的其他子域/主站点上正常使用 mysqli 连接,但我无法让 PDO 工作。

我试过这个,我见过:

 $stmt = $dbh->prepare('SELECT * FROM gamersite.USERS WHERE ID = :id LIMIT 1');

但它会返回语法错误。

任何人都知道可能导致这种情况的原因是什么?


这一切都在我的本地服务器上运行,除了连接线外,上传没有任何变化。

4

2 回答 2

12

代替:

$dbh = new PDO("mysql:91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");

尝试:

$dbh = new PDO("mysql:host=91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");

(添加主机=

它很可能在您的本地服务器上工作,因为您拥有mysql:localhost...mysql:127.0.0.1...在那里并且它被忽略(因为它也缺少主机=)并且默认情况下它是本地主机。

于 2013-08-28T22:16:30.870 回答
1

From the PDO manual page, you can see that you need to wrap the connection in a try/catch block. This way if something goes wrong with the connection, it will tell you. Something like this:

try {
    $dbh = new PDO("mysql:91.146.107.11;dbname=kennyi81_gamersite", "kennyi81_gamer", "***************");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";

    // Then actually do something about the error
    logError($e->getMessage(), __FILE__, __LINE__);
    emailErrorToAdmin($e->getMessage(), __FILE__, __LINE__);
    // etc.
    die(); // Comment this out if you want the script to continue execution
}

The reason you are getting this error is because there is an error with your connection, but since you don't tell your script to stop, it doesn't. Look at the error message produced, and how to fix it should be made obvious. It appears that Michael Prajsnar's answer is correct in that you aren't setting a "host".

Edit:

As it turns out, PDO doesn't complain if you leave out your host or dbname in the PDO connection DSN part (at least on Unix). I tested it and leaving it blank will default it to "localhost" and I was therefore able to connect perfectly fine leaving this out completely for localhost connections, which would explain why it worked on your local server but not on your production server. In fact, it is completely possible to connect supplying absolutely nothing in the DSN except for the database engine like this:

$dbh = new PDO("mysql:", "kennyi81_gamer", "***************");

The only problem is that it won't be using a database, so to USE a database, just do:

if ($dbh->query("USE kennyi81_gamersite") === false)) {
     // Handle the error
}

However with that said, I have my doubts that you actually tried connecting using a try/catch block (as you mention in your comments) unless you somehow provided valid database credentials. The ONLY way that doing it this way did not produce any sort of error is if you actually connected correctly and selected the database kennyi81_gamersite. If not, you would have seen a message like this:

Unable to connect to database. "mysql" said: SQLSTATE[28000] [1045] Access denied for user 'kennyi81_gamer'@'localhost' (using password: YES)

In summary, always wrap your connection in a try/catch block if you want to find errors during connection. Just make sure not to re-throw (and not catch) the PDOException's getMessage() or you could expose your login credentials.

于 2013-08-28T22:15:51.617 回答