1 00:00:00,730 --> 00:00:05,100 Now that you have at least a basic understanding of objects, methods, 2 00:00:05,100 --> 00:00:09,490 properties, and classes, you're ready to write code to instantiate or 3 00:00:09,490 --> 00:00:14,070 create an instance of an object, using the PDO class. 4 00:00:14,070 --> 00:00:17,380 We'll then use this object to connect to our database. 5 00:00:17,380 --> 00:00:18,440 Let's go back to our workspace. 6 00:00:19,970 --> 00:00:23,580 Let's create a file we can use to set up the code for connecting to the database. 7 00:00:24,580 --> 00:00:26,978 Let's add this file in the includes directory. 8 00:00:30,660 --> 00:00:35,820 And we'll name it connection.php. 9 00:00:35,820 --> 00:00:39,830 To create an object from a class, you first specify a variable name for 10 00:00:39,830 --> 00:00:41,000 the object you want to create. 11 00:00:42,340 --> 00:00:48,440 Let's open our php, and then create db for database. 12 00:00:48,440 --> 00:00:50,610 We want to assign this variable a value. 13 00:00:51,780 --> 00:00:53,200 So we use the single equal sign. 14 00:00:55,290 --> 00:00:57,800 To create a new object, we use the keyword new. 15 00:00:59,350 --> 00:01:03,446 We then specify the class name, followed by an open parenthesis. 16 00:01:03,446 --> 00:01:06,670 PDO open parenthesis, 17 00:01:06,670 --> 00:01:11,630 creating a new object from a class calls a function or method inside the class. 18 00:01:11,630 --> 00:01:15,650 So use parenthesis after the class name, like you do with functions and 19 00:01:15,650 --> 00:01:16,970 other methods. 20 00:01:16,970 --> 00:01:18,210 Just like with functions, 21 00:01:18,210 --> 00:01:23,250 when you create a new object from a class, you can often pass in arguments. 22 00:01:23,250 --> 00:01:24,670 With the PDO class, 23 00:01:24,670 --> 00:01:28,180 you pass in the information needed to connect to the database. 24 00:01:28,180 --> 00:01:30,910 We'll be using SQLight for the following example. 25 00:01:30,910 --> 00:01:33,220 So let's see what's needed for that database. 26 00:01:34,240 --> 00:01:37,050 We'll do that by opening a new tab in our browser. 27 00:01:38,940 --> 00:01:41,950 We'll do a search for pdo drivers. 28 00:01:43,700 --> 00:01:47,710 A driver is a piece of software that connects one thing to another. 29 00:01:47,710 --> 00:01:48,870 In this case, we're looking for 30 00:01:48,870 --> 00:01:53,400 the PDO driver that connects SQLight to PHP using PDO. 31 00:01:54,470 --> 00:01:56,010 The first result is the one we want. 32 00:01:57,850 --> 00:02:01,470 You can see here that we have several different PDO drivers. 33 00:02:01,470 --> 00:02:03,400 We're going to want SQLite. 34 00:02:03,400 --> 00:02:07,560 So let's click on SQLLite And let's see what that requires. 35 00:02:07,560 --> 00:02:11,450 It says that PDO_SQLITE is a driver that implements the PHP 36 00:02:11,450 --> 00:02:17,160 data objects interface to enable access to SQLite three databases. 37 00:02:17,160 --> 00:02:20,370 In order to do that, we're going to need a certain string. 38 00:02:20,370 --> 00:02:22,950 We need this SQLITE DSN. 39 00:02:24,880 --> 00:02:27,110 So we will need the data source name, 40 00:02:27,110 --> 00:02:32,780 which is SQLite, to access a database on disk, which is what we have. 41 00:02:32,780 --> 00:02:38,000 We can see that we need to append the absolute path to the DSN prefix. 42 00:02:38,000 --> 00:02:41,260 So we need SQLite, followed by the path to the database. 43 00:02:42,330 --> 00:02:44,760 Okay, let's go make this work. 44 00:02:44,760 --> 00:02:48,950 Within our call to the PDO class, we're going to add SQLite within quotes, 45 00:02:51,510 --> 00:02:55,200 followed by a colon and the path to our database. 46 00:02:56,250 --> 00:03:01,565 Instead of the full path, we're going to use a special magic constant that php has. 47 00:03:01,565 --> 00:03:07,538 _ _DIR_ _, this means to look in the exact 48 00:03:07,538 --> 00:03:12,740 directory where this file is located. 49 00:03:14,330 --> 00:03:18,596 We're then going to use /database.db, 50 00:03:18,596 --> 00:03:22,630 close the parentheses and end the line. 51 00:03:23,920 --> 00:03:28,690 If you wanna learn more about magic constants, check out the teacher's notes. 52 00:03:28,690 --> 00:03:30,040 We're gonna save this file. 53 00:03:30,040 --> 00:03:32,540 And just to make sure that we've created our new object, 54 00:03:32,540 --> 00:03:37,240 we're going to do a var_dump. 55 00:03:37,240 --> 00:03:40,760 We're gonna dump the actual database object we created. 56 00:03:40,760 --> 00:03:42,700 We'll save this and preview in the browser again. 57 00:03:43,760 --> 00:03:51,140 We're going to have to type in inc/connection.php. 58 00:03:51,140 --> 00:03:55,500 Okay so here we see that it's dumping an object, it's a PDO object. 59 00:03:56,920 --> 00:03:59,950 The PDO object is connecting to our database but 60 00:03:59,950 --> 00:04:04,250 sometimes, you might have a typo in your code or, for some other reason, 61 00:04:04,250 --> 00:04:07,340 PHP may not be able to connect to your database. 62 00:04:07,340 --> 00:04:11,270 In the next video, I'll show you how to tackle unexpected problems and 63 00:04:11,270 --> 00:04:12,540 handle errors gracefully.