Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Getting Started with PDO4:13 with Alena Holligan
The PHP Data Object (PDO) allows PHP to interact with a database. This project uses a SQLite database, but you can connect to many different types of databases using a PDO. This video will show you how to find and use the documentation for PDO so you can then use that documentation to connect to other databases as well.
PHP provides a large number of predefined constants to any script which it runs. More information on these Magic Constants can be found here.
Connecting PDO to MySQL
$db = new PDO("mysql:host=localhost;dbname=DATABASE_NAME;port=3306","USERNAME","PASSWORD");
For MySQL, you specify mysql and a colon, then type "host=" followed by the name of the host to which you want to connect. After that, you type a semicolon, followed by dbname and an equal sign, followed by the name of the database. If your environment is using the default MySQL port of 3306, this string here might be all you need.
MAMP uses a different port (8889 by default), so if you haven't changed the default, you will have to specify the port number. You type another semicolon, followed by the word "port" and an equal sign, followed by the port number.
That's the first argument to pass in when instantiating a new object from the PDO class. It's all surrounded by 1 set of quotation marks because it's 1 PHP string.
The word “host”, here, is not a PHP variable— it's just part of a piece of text that gets used to identify the database.
For MySQL, the second argument is the username and the third argument is the password. By default, the MySQL username and password on MAMP are both "root".
Now that you have at least a basic understanding of objects, methods, 0:00 properties, and classes, you're ready to write code to instantiate or 0:05 create an instance of an object, using the PDO class. 0:09 We'll then use this object to connect to our database. 0:14 Let's go back to our workspace. 0:17 Let's create a file we can use to set up the code for connecting to the database. 0:19 Let's add this file in the includes directory. 0:24 And we'll name it connection.php. 0:30 To create an object from a class, you first specify a variable name for 0:35 the object you want to create. 0:39 Let's open our php, and then create db for database. 0:42 We want to assign this variable a value. 0:48 So we use the single equal sign. 0:51 To create a new object, we use the keyword new. 0:55 We then specify the class name, followed by an open parenthesis. 0:59 PDO open parenthesis, 1:03 creating a new object from a class calls a function or method inside the class. 1:06 So use parenthesis after the class name, like you do with functions and 1:11 other methods. 1:15 Just like with functions, 1:16 when you create a new object from a class, you can often pass in arguments. 1:18 With the PDO class, 1:23 you pass in the information needed to connect to the database. 1:24 We'll be using SQLight for the following example. 1:28 So let's see what's needed for that database. 1:30 We'll do that by opening a new tab in our browser. 1:34 We'll do a search for pdo drivers. 1:38 A driver is a piece of software that connects one thing to another. 1:43 In this case, we're looking for 1:47 the PDO driver that connects SQLight to PHP using PDO. 1:48 The first result is the one we want. 1:54 You can see here that we have several different PDO drivers. 1:57 We're going to want SQLite. 2:01 So let's click on SQLLite And let's see what that requires. 2:03 It says that PDO_SQLITE is a driver that implements the PHP 2:07 data objects interface to enable access to SQLite three databases. 2:11 In order to do that, we're going to need a certain string. 2:17 We need this SQLITE DSN. 2:20 So we will need the data source name, 2:24 which is SQLite, to access a database on disk, which is what we have. 2:27 We can see that we need to append the absolute path to the DSN prefix. 2:32 So we need SQLite, followed by the path to the database. 2:38 Okay, let's go make this work. 2:42 Within our call to the PDO class, we're going to add SQLite within quotes, 2:44 followed by a colon and the path to our database. 2:51 Instead of the full path, we're going to use a special magic constant that php has. 2:56 _ _DIR_ _, this means to look in the exact 3:01 directory where this file is located. 3:07 We're then going to use /database.db, 3:14 close the parentheses and end the line. 3:18 If you wanna learn more about magic constants, check out the teacher's notes. 3:23 We're gonna save this file. 3:28 And just to make sure that we've created our new object, 3:30 we're going to do a var_dump. 3:32 We're gonna dump the actual database object we created. 3:37 We'll save this and preview in the browser again. 3:40 We're going to have to type in inc/connection.php. 3:43 Okay so here we see that it's dumping an object, it's a PDO object. 3:51 The PDO object is connecting to our database but 3:56 sometimes, you might have a typo in your code or, for some other reason, 3:59 PHP may not be able to connect to your database. 4:04 In the next video, I'll show you how to tackle unexpected problems and 4:07 handle errors gracefully. 4:11
You need to sign up for Treehouse in order to download course files.Sign up