0

I just start learning about PDO, I need some help, I Installed PHPStorm, and I just start using it too, I already have a datbase on phpMyAdmin, I made this code, but it gives me an error

 <?php try { $handler = new PDO ('mysql:localost;dbname=Database', 'root', 'password'); $handler -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOExeption $e) { die('Sorry, Database problem'); } $query = $handler->query('select * from users'); while($r= $query->fetch()) { echo $r['name']; } ?> 

here is the error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in C:\Users\user1\PhpstormProjects\PDO\pdo.php:11 Stack trace: #0 C:\Users\user1\PhpstormProjects\PDO\pdo.php(11): PDO->query('select * from u...') #1 {main} thrown in C:\Users\user1\PhpstormProjects\PDO\pdo.php on line 11. Any help?

thanks in advance :).

1
  • @CD001 Lol, I feel so stupid Commented Aug 25, 2015 at 15:40

2 Answers 2

2

Please make sure your database actually exists and that there are no spelling mistakes in your connection code.

Edit 1

I also noticed this issue with your connection I'm not sure if this is what is causing it but you need mysql:host= not just mysql:localhost. Also you have a spelling mistake localost.

Change this,

$handler = new PDO ('mysql:localost;dbname=Database', 'root', 'password'); 

To,

$handler = new PDO('mysql:host=localhost;dbname=myDb', $username, $password); 
Sign up to request clarification or add additional context in comments.

Comments

0

replace the handler declaration for this:

$handler = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password); 

where myDatabase is the one you will be using as default..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.