Using PHP’s PDO Class to Connect to a Database

This lesson is exclusive to members

Questions

Course info

13 lessons / 1 hours

Overview

Hi, In this course we are going to add a newsletter sign up form that will save our new subscribers to a database.

To do this we are going to be using a backend programming language called PHP and a MySQL database.Don’t worry if that doesn’t make any sense to you, we will be going over all of the basics and walking you through it step by step.

This course does however assume you have a basic understanding of html and css, and that you know how to push any updates up to a server and make them live.

If you don’t have that experience then I can recommend this course by Daniel Scott, it starts at the very beginning and will easy get you up to speed so you can start taking the next steps like working with PHP and databases.

So if you’re ready to get started with our PHP database project … then sign up to the course and lets get started :)
Daniel Scott

Daniel Scott

Founder of Bring Your Own Laptop & Chief Instructor

instructor

I discovered the world of design as an art student when I stumbled upon a lab full of green & blue iMac G3’s. My initial curiosity around using the computer to create ‘art’ developed into a full-blown passion, eventually leading me to become a digital designer and founder of Bring Your Own Laptop.

Sharing and teaching are a huge part of who I am. As a certified Adobe instructor, I've had the honor of winning multiple Adobe teaching awards at their annual MAX conference. I see Bring Your Own Laptop as the supportive community I wished for when I was first starting out and intimidated by design. Through teaching, I hope to bring others along for the ride and empower my students to bring their stories, labors of love, and art into the world.
True to my Kiwi roots, I've lived in many places, and currently, I reside in Ireland with my wife and kids.

Downloads & Exercise files

Transcript

In this video we are going to add in the PHP required  to connect to our database. We are gonna go through it line by line  so we know what's happening  and we are also gonna test that it's working correctly. Okay, so before we start saving anything to our database,  the first thing that we're gonna need  to do is create a database connection. So if we just update this, so  before it was submitting to our newsletter example,  let's submit to newsletter signup  and we'll create a new PHP file called  Newsletter Signup. Oh, let's copy that. New file newsletter signup php.

And I'm just gonna again, copy over magically some PHP. Now if we go back to the browser  and open up local host 80 80  and go to PHP my admin. And if you remember, this is what we used  to manage our database. And just to refresh our memory, we had a meat yogurt  database that we added a newsletters table two. So if we go back to that PHP script that I just pasted in  and just have a quick look at what's going on here. So we've got a variable here, which is DB for our database.

And what this is doing is creating a new  database connection. PDO is what PHP uses to connect to the database. And this is the information that we need to pass in  to create that database connection. So my SQL is the type of database that we are using. The host is the IP address of the database  that we're connecting to. The database name is Meet Yogurt,  which we saw over here in PHP.

My admin, we've got Meet Yogurt  and root is the username. And we don't have a password set,  so we just leave it as blank. So what this line here is trying to do is trying, is trying  to connect to the database. And if that fails, it's gonna catch it here  and output an error message for us. So if we come down here  and maybe if we just print our,  our, our database just so we can see what's going on,  if we go back to our index php, check that,  that's submitting to the signup, we'll save that, go over  to our website to refresh the page. And let's see what happens when we submit it this time.

I thought that that might show us a bit more information  about our database connection, but it looks like it doesn't. But let's just go back to VS code. And that's the line that we were seeing. So we know that a database connection is working  because it's not displaying this error message. Now, with the connection info, most  of the time the only parts that you're gonna need  to change are the database name  and we're using root in a blank password  because that's just the default that exam uses. When you actually upload your project to a server,  you're gonna have a different username and password,  but we'll cover that later on.

So for now, let's just see what happens when we break it. So let's, um, leave the t off the end of our database name,  go back to our website and refresh the page. And we can see that now we're not connecting  and we are seeing the error message. Let's un comment this line  so we can see a little bit more information  about why we're not connecting. So it's telling us that unknown database  'cause it's s spelled wrong. So if you go back to VS code, so this is, um,  catching the PDO exception from the database.

So it's saying that something went wrong. Let's, let's catch the error. And we are using a get message method on that error. So what we can do is get rid of that  and that will show us a lot more information. And again, if we use our pret tags that we saw before. So if we echo out pre tag here  and then we'll put one underneath too.

Go back to our website. So now we can see this was the message that we were seeing  before about the unknown database. There's just some other info here that might be handy  for you when you're trying to do some debugging. So we know that this is working  when we put the T back on  because we're not seeing this error message. And we'll leave this but commented out  because if there was to be a problem on our website,  we don't want our users to be able to see this. We just want 'em to see this message  and give them the option to go back to the homepage.

Okay, now that we have our database connection,  in the next video we'll take the information from our form  and add that into our database.
  • Powered by Marvin
  • Terms of use
  • Privacy policy
  • © Bring your Own Laptop Ltd 2025