1 00:00:00,570 --> 00:00:05,300 The shell of the project is all set up for you including CSS styles and forms. 2 00:00:05,300 --> 00:00:08,160 There's also a SQLite database with all the tables and 3 00:00:08,160 --> 00:00:11,420 fields that you need as well as some of the initial test data. 4 00:00:11,420 --> 00:00:15,090 In this video we use PHPs data object or PDO for 5 00:00:15,090 --> 00:00:18,210 short, to connect the application to the database. 6 00:00:18,210 --> 00:00:20,974 For accessing the projects and test data. 7 00:00:20,974 --> 00:00:24,770 You should already be familiar with setting up a connection to the database. 8 00:00:24,770 --> 00:00:28,080 But we'll cover everything that you need to know in this video. 9 00:00:28,080 --> 00:00:30,540 If you'd like more details than what's covered here, 10 00:00:30,540 --> 00:00:33,200 check out the notes associated with this video. 11 00:00:33,200 --> 00:00:35,530 We're going to write code to instantiate or 12 00:00:35,530 --> 00:00:39,510 create an instance of an object from the PDO class. 13 00:00:39,510 --> 00:00:42,720 This object variable gives us all the attributes and 14 00:00:42,720 --> 00:00:45,740 actions that we need to interact with the database. 15 00:00:45,740 --> 00:00:46,890 Let's go into workspaces. 16 00:00:48,320 --> 00:00:51,140 Let's create a new file for connecting to the database. 17 00:00:51,140 --> 00:00:54,600 Let's add this file to the includes directory, inc for short. 18 00:00:55,770 --> 00:01:00,833 We'll name this file connection.php. 19 00:01:00,833 --> 00:01:04,592 To create an object from a class, you first specify a variable name for 20 00:01:04,592 --> 00:01:06,930 the object you want to create. 21 00:01:06,930 --> 00:01:10,340 We'll call our object $db for database. 22 00:01:10,340 --> 00:01:12,760 We want to assign this variable a value. 23 00:01:12,760 --> 00:01:14,060 So we're using equal sign. 24 00:01:15,790 --> 00:01:19,740 To create a new object, we use the keyword new. 25 00:01:19,740 --> 00:01:23,300 We then specify the class name followed by parenthesis. 26 00:01:24,310 --> 00:01:29,310 This creates a new empty object variable of the type PDO 27 00:01:29,310 --> 00:01:32,580 just like calling the function array creates an empty array. 28 00:01:33,580 --> 00:01:35,190 Also like the array function, 29 00:01:35,190 --> 00:01:38,558 you can often pass arguments when you create a new object. 30 00:01:38,558 --> 00:01:40,650 With a PDO object, 31 00:01:40,650 --> 00:01:44,510 you pass in the information needed to connect to the database. 32 00:01:44,510 --> 00:01:47,180 We'll be using SQLite for the following example. 33 00:01:47,180 --> 00:01:51,050 So let's check out the documentation to remind us about what is needed for 34 00:01:51,050 --> 00:01:51,710 that database. 35 00:01:52,910 --> 00:01:56,930 In your browser search for pdo sqlite. 36 00:01:56,930 --> 00:01:59,130 The first result is the one we want. 37 00:02:00,210 --> 00:02:05,030 PDO SQLite is a driver that implements the PHP data object 38 00:02:05,030 --> 00:02:08,400 to enable access to SQLite 3 databases. 39 00:02:08,400 --> 00:02:12,890 A driver is a piece of software that connects one thing to another. 40 00:02:12,890 --> 00:02:19,710 In this case, the PDO driver connects SQLite to PHP using the PHP data object. 41 00:02:20,750 --> 00:02:26,160 In the Table of contents the first link is for PDO SQLLite DSN. 42 00:02:26,160 --> 00:02:28,360 in order for 43 00:02:28,360 --> 00:02:34,010 PDO to use the SQLLite database, we need to pass the data source name. 44 00:02:34,010 --> 00:02:36,140 To access a database on disk, 45 00:02:36,140 --> 00:02:41,500 we pass the prefix SQLite followed by the absolute path to the database. 46 00:02:41,500 --> 00:02:44,515 Okay, let's go back and add this to our connection. 47 00:02:44,515 --> 00:02:51,140 Within our call to the PDO class we're going to add sqlite;. 48 00:02:51,140 --> 00:02:55,870 And in our case the database is in the include folder as well. 49 00:02:55,870 --> 00:03:01,837 So we're going to use the magic constant 50 00:03:01,837 --> 00:03:07,811 __DIR__ and then /database.db. 51 00:03:07,811 --> 00:03:11,320 On the next line, we're going to var dump our db variable. 52 00:03:13,610 --> 00:03:16,670 This will make sure that we've created our new object. 53 00:03:16,670 --> 00:03:19,836 Let's run this file in the console to see what's going on. 54 00:03:19,836 --> 00:03:22,275 View > Show Console. 55 00:03:22,275 --> 00:03:29,296 Type php inc/connection.php. 56 00:03:29,296 --> 00:03:31,368 We see that it's dumping an object. 57 00:03:31,368 --> 00:03:33,358 It's a PDO object. 58 00:03:33,358 --> 00:03:39,340 So our PHP code is now connecting to an external system, a SQLite database. 59 00:03:39,340 --> 00:03:42,750 But what if we make a mistake, like a spelling error. 60 00:03:42,750 --> 00:03:47,243 Let's change SQLite to have two Ls since that's the way I wanna type it anyway. 61 00:03:52,936 --> 00:03:56,270 Now we get a bunch of errors and we don't want that. 62 00:03:56,270 --> 00:03:59,346 So the next thing for us to do is a little bit of error handling.