Connecting mysql to php

I’m learning php and I’m having trouble connecting to a database in mysql. my code - https://pastebin.com/SV0cTbcd I’m not sure what I’m doing wrong here or how to trouble shoot this since no error is showing up, I’m only having the error being catched so in my browser ‘cant connect’ keeps showing up instead of whats in my database. My dbname is correct so I think something is wrong with the mysql:host=127.0.0.1, but I’m not sure and don’t know how to check how to fix it. I’d appreciate any help with this.

EDIT when I changed <?php 'mysql:host=localhost; ? > this line of code to localhost instead of 127.0.0.1, I’m not catching an error anymore, but my rows in my database aren’t showing and I’m not sure why. Is there any way to confirm my database is connecting correctly or am I printing the database rows incorrectly?

Hi @aioy,

Based on your description, it looks like the problem occurs when you are trying to connect to your database.
To get a better idea what is going wrong, you could change the error you are catching. If you change the following lines:

} catch(PDOException $e){

die('cant connect');

}

to

} catch (PDOException $e) {
die($e->getMessage());
}

You should be able to get more details on the problem.

In the meantime you should double check your username and password as well. In many cases, the problem is caused by these two.

Yeah I solved that and it was a permissions error and I fixed it by changing the user in pdo to root. The new code looks like this `try{

$pdo = new PDO(‘mysql:host=127.0.0.1;dbname:mytodo’, ‘root’, ‘’);

} catch(PDOException $e){

die('cant connect' . $e->getMessage());

}

$statement = $pdo->prepare(‘select * from todos’);

$statement->execute();

var_dump($statement->fetchAll());`

But I’m still seeing nothing in my browser except the html text 'array(0) { } ’

In my terminal my mysql tabels shows this

'mysql> show tables;
+------------------+
| Tables_in_mytodo |
+------------------+
| todos            |
+------------------+
mysql> select * from todos;
+----+-------------+-----------+
| id | description | completed |
+----+-------------+-----------+
|  1 | nihongo genki |         1 |
|  2 | wakaru      |         0 |
+----+-------------+-----------+'

So I’m expecting to see nihongo genki and wakaru to be printed in my browser but I see nothing and still don’t know how to fix this. I’m not getting any errors shown either.

I solved it. The error wasn’t showing up because it was a string I guess, but I’m not sure how to debug this since I’m just learning php. It was a typo here ;dbname:mytodo’, should have been dbname=mytodo, and after I fixed that its working. Thanks for trying to help tomvbe.