CamKem's avatar
Level 10

Issue completing instructions in PDO lesson.

Im doing the php beginners course for a refresher after a 20 year hiatus from php and I'm having issues with the PDO / mysql lesson.

This is the lesson -> https://laracasts.com/series/php-for-beginners-2023-edition/episodes/17?page=1

My code looks like this

$dsn = "mysql:host=localhost;port:3306;dbname=aur;user=root;charset=utf8mb4";
$pdo = new PDO($dsn);

$statement = $pdo->prepare("select * from posts");
$statement->execute();

$posts = $statement->fetchAll();

dd($posts);

The error I'm getting is the following (database does not exist)

Fatal error: Uncaught PDOException: SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

Please help.

0 likes
4 replies
jlrdw's avatar

Try:

$pdo = new \PDO($dsn);

And put password as ''.

CamKem's avatar
Level 10

@jlrdw Now its saying (Access denied for user 'root'@'localhost' (using password: YES))

$dsn = "mysql:host=localhost;port:3306;dbname=aur;user=root;password=.;charset=utf8mb4";
$pdo = new \PDO($dsn);

$statement = $pdo->prepare("select * from posts");
$statement->execute();

$posts = $statement->fetchAll();

dd($posts);
jlrdw's avatar

@CamKem okay try leaving password out then. I always use one so I'm not sure how to use none in the dsn line.

Double check the video.

CamKem's avatar
CamKem
OP
Best Answer
Level 10

I'm not sure why the code from the video wont work for me.

I just referred to the php documentation and typed the code out again using the following and it worked.

$dsn = 'mysql:dbname=aur;host=127.0.0.1';
$user = 'root';
$pdo = new PDO($dsn, $user);

Thanks anyway!

Please or to participate in this conversation.