Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJason Chiu
4,373 PointsA fatal problem to connect to mysql database with PDO class
Hi,
I was trying to connect to localhost/database.php with PDO class but got a fatal error on PDO exception. The error is:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\xampp\htdocs\database.php:3 Stack trace: #0 C:\xampp\htdocs\database.php(3): PDO->__construct('mydql:host=loca...', 'root', 'root') #1 {main} thrown in C:\xampp\htdocs\database.php on line 3
I did not install the complete package of XAMPP, but instead installed mysql as a standalone package. I only installed php and apache from XAMPP.
Also here are my codes for php:
<?php
$db = new PDO("mydql:host=localhost;dbname=shirts4mike;port=3306", "root", "root");
var_dump($db);
?>
Anyone knows how to solve the problem? Please help. Thanks!
2 Answers
Chris Shaw
26,676 PointsHi Jason,
You have a very minor typo which is instead of mysql you have mydql, simply change the d to an s and the error should go away.
Happy coding!
Christopher Parke
21,978 PointsI'm unfortunately getting the same error. I'm using Xampp.
<?php
$db= new PDO("mysql:host=localhost;dbname=shirts4mike;port=8889","root", "");
var_dump($db);
Christopher Parke
21,978 PointsI solved it. The issue is that MySQL uses a different port. I found the port it uses by opening xampp