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,622 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,977 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,977 Points

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