Connecting to MySQL with PDO

With MySQLi, it looks like this:

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

But, PDO is a bit different because it can interact 12 different types of databases: Oracle, PostgreSQL, SQLite, MySQL, Cubrid and several others. So, when using it you have to specify which driver you want to use.

Like this:

$conn = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);

Notice the “mysql:host=” bit.

For different drivers, you just change out the “mysql” part.

So, PostgreSQL would be:

$conn = new PDO("pgsql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);

For Oracle, it’d be:

$conn = new PDO("oci:host={$db_host};dbname={$db_name}", $db_user, $db_pass);

And, so on.

Nice thing is…

The code after this doesn’t change for each kind of database. So, you could write the rest of your database interaction code initially for MySQL… then, change to say PostgreSQL.. .and all you have to do is change out the drive designation (mysql, pgsql, oci, etco).

That’s why you hear people recommending you use PDO a lot.

If there’s any chance you might switch to a different database…

It makes sense.

Anyway, this one of the many things you’ll learn inside Module 3 of PHP 101 (which I just uploaded to the site). Along with the basics of CRUD, you’ll learn MySQLi and PDO, prepared statements, building a database class, submitting an HTML for to your database (AND getting data back from it)… and more.

Right now, you can grab all 3 modules for just 27 buqs.

That’s 10 buqs off the regular fee.

Use this link to get it: http://johnmorrisonline.com/php

Later,

John

You might also like

Hack Culture Needs to Die

Anybody else tired of “hack” culture? “7 Hacks to Triple Your…” “A Secret Hack to Instantly Double Your…” “52 Little-Known Hacks to…” I asked my

Read More »
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

The simple secrets to high-paying freelance clients

What makes clients willing to pay $5,000, $10,000 even $20,000 and up for your services? Download and install my mobile app and I’ll show you. It’s free. Just click the button below:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Michael Skye

Michael Skye

John is a man of integrity, who gives generously of himself to projects and people he cares about.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Oliver Wainwright

Oliver Wainwright

I’m a fan. I have completed several of John’s courses. I find him very knowledgeable and he has a great delivery.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Aaron Gott

Aaron Gott

John has a particular knack for the development and training of others.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.