Mysql-PDO错误-无效的目录名称:1046未选择数据库 [英] Mysql - PDO Error - Invalid catalog name: 1046 No database selected
问题描述
我对PDO有问题,而且我绝对看不到他的来历.我不能质疑我的MySQL数据库.为了进行测试,我使用了以下代码(之前为连接配置的参数非常糟糕:
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');
使用MySQL正确建立连接,但查询后出现此错误:
The connection is made correctly with MySQL but after query I get this error:
致命错误:/home/outout/public_html/file.php:16堆栈跟踪:#0/home/outout中未捕获的异常'PDOException',消息为'SQLSTATE [3D000]:无效的目录名称:1046未选择数据库' /public_html/file.php(16):PDO-> query(从*中选择* ...")#1 {main}放在第16行的/home/outout/public_html/file.php中.
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.
该代码可在本地计算机上运行,但是一旦我将其联机(cPanel),它就会向我显示此错误.我必须在.htaccess中配置PDO吗?
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?
推荐答案
帮助Mysql解析句柄.
Help Mysql resolve the handle by.
代替:
$res=$dbh->query('SELECT * FROM table');
尝试:
$res=$dbh->query('SELECT * FROM Mydatabase.table');
这篇关于Mysql-PDO错误-无效的目录名称:1046未选择数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!