Why is my PDO not working?
mysql, pdo, php
Solution
You need to tell PDO that you want it to throw exceptions:
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Following your comment below, it is apparent that your DSN is incorrect. It should be:
$connection = new PDO('mysql:host=localhost;dbname=my_db','my_username','xxxxxxx');
Note that the syntax is `dbname=` rather than `dbname:` (which you had originally).
Problem
I am starting to use PDO and I successfully connected to MySQL using PDO. However, when I try to SELECT stuff from my DB, nothing happens. Nothing is echoed. (even though I have records in that table, and the column username exists) No error in my PHP log. I am using MAMP and all PDO components seem to be working in phpinfo() (since I was able to connect to db in the first place) Please let me know what could have gone wrong. Thanks a lot ``` <?php try { $connection = new PDO('mysql:host=localhost;dbname:my_db','my_username', 'xxxxxxx'); $stmt=$connection->prepare("SELECT * FROM users"); $stmt->execute(); while ($row=$stmt->fetch(PDO::FETCH_OBJ)){ echo $row->username; } } catch(Exception $e) { echo "There was an error connecting to the database"; } ?> ```