I'm just learning to use PDO to manipulate the database, but, after connecting to the database, use Query () to execute the SELECT query statement, but always error: No database selected, code and error information as follows:
<?php
Header ("Content-type:text/html;charset=utf-8");
$dsn = ' Mysql:dbname =pdo;host=localhost ';
$user = ' root ';
$pass = ' 086437 ';
$DBH = new PDO ($DSN, $user, $pass);
$DBH->setattribute (pdo::attr_errmode,pdo::errmode_exception);
$sql = ' SELECT * from PDO ';
$pdos = $dbh->query ($sql);
>
the error is:
Fatal error:uncaught exception ' pdoexception ' with message ' sqlstate[3d000]: Invalid Catalog No Database Selected ' in D:\wamp\www\PDO\select.php on line
-
pdoexception:sqlstate[3d000]: Invalid catalog name:1046 No Database selected in D:\wamp\www\PDO\select.php on line
-
The error message is not connected to the database, but I clearly chose the $DSN = ' mysql:dbname =pdo;host=localhost ';Finally tried for a long time to find the original is Mysql:dbname =pdo; This sentence mysql:dbname should not be followed by a space, should be: mysql:dbname=pdo; I hope this experience will help you.