Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Development Tools Using PHP with MySQL Connecting PHP to MySQL Getting Started with PDO

A 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
Chris Shaw
26,649 Points

Hi 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
Christopher Parke
21,978 Points

I'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
Christopher Parke
21,978 Points

I solved it. The issue is that MySQL uses a different port. I found the port it uses by opening xampp